How to automates test cases for Android apps(UI) using the mock api responses in espresso with Java?

Viewed 62

I want to automate specific test cases on android UI, where I need to mock up the responses, However, i have the normal test where I will hit the server and execute the flow.

Currently i have stored the api response in the String and passing

String apiResponse= // here i gave the json response

 public void populateAPIResponse(String apiResponse) {
        product = new Product();// Product is the class where serilized response is stored
        try {
            Gson gson = new Gson();
            product = gson.fromJson(apiResponse, Product.class);
            productContract = (ProductContract) product;
        } catch (Exception ignored) {
        }
    }

Here i have the deserialized class details ( which is maintaining by the Android development team , for testing we are reusing it)

  @JsonIgnoreProperties(ignoreUnknown = true)
    class Product() : ProductContract, IAcceptableResponse {
    
        @SerializedName("code")
        var productCodeId: String? = null
    
        @SerializedName("productId")
        var prodId: String? = null
}

Below is the Contract for the above class

   public interface ProductContract extends Parcelable {
       
        String getCode();
    
        String getProductId();
}

In the above approach, i can be able to store& get the required response in the POJO class, However when I perform any action on the app locator( i.e performing add to cart a product) then the app is hitting the server for response , is there any alternative way where it can pick the mock response whatever i gave?

0 Answers
Related