Creating http response as a return value for mocki

2019-06-02 06:56发布

问题:

I am new to Mockito testing and am testing a GET request.

I have mocked the call but now I need to return HttpResponse<Cars[]> because these values are later used in for loop.

How can I create a dummy HttpResponse where the body contains the array of Car objects.

The method where Get request is used in class CarProvider is:

public HttpResponse<Cars[]> getCars(String carId, String carname) {
    return Unirest.get(endpointUrl)
          .header("Accept", MediaType.APPLICATION_JSON)
          .queryString("carId",cardId)
          .queryString("carname",carname)
          .asObject(Cars[].class);
}

This is used in another class CarsUsers in below method:

public List<Cars> getCars(String carId, String carname) {
  HttpResponse<Cars[]> response = carProvider.getCars(carId, carname);
  for(Cars car: response.getBody){
     //use car and do something
}

My test method is as follows:

public class TestClass {
    @Mock
    HttpResponse<Cars[]> httpresponse;
    @Mock
    CarsProvider carsProvider;

    @Mock
    CarsUser carsUser;

    @Test
    public void getCarsTest(){
     Mockito.when(carsUser.getCars(Matchers.anyString(), 
     Matchers.anyString())).thenReturn(getDummyCarsList());

     Mockito.when(carsProvider.getCars(Matchers.anyString(), 
     Matchers.anyString())).thenReturn(httpResponse);

     }

    private List<Cars> getDummyCarsList(){
    //create a dummy list of cars
    }
}

I get NullPointerException pointing to for(Cars car: response.getBody){}.

I am assuming that body doesnt have any value and hence the exception is thrown.

Appreciate any help or suggestions.

Thanks

回答1:

Do you really need CarProvider to return a HttpResponse<Car[]>?

It feels like an aspect of the underlying comms (HttpResponse) might be leaking here. If the purpose of the CarProvider is to provide cars then perhaps it should be typed accordingly.

So, if you declared CarProvider as follows ...

public class CarProvider {
    // should perhaps consider List<Car> instead of Car[] here ...
    public Cars[] getCars(String carId, String carname) {
        HttpResponse<Car{}> response = Unirest.get(endpointUrl)
          .header("Accept", MediaType.APPLICATION_JSON)
          .queryString("carId",cardId)
          .queryString("carname",carname);

        return deserialise(response.getBody());
    }

    private Car[] deserialise(ResponseBody body) {
        // read the body and deserialise to Car[] 
    }
}

... then your test method would simplify to:

@Test
public void getCarsTest(){
    Mockito.when(carsUser.getCars(anyString(), anyString())).thenReturn(getDummyCarsList());

    // ...
}

private Cars[] getDummyCarsList(){
    return new Car{} {new Car(...), new Car(...)};
}

However, if this really is not possible and you really must mock HttpResponse<Car[]> then that'll look something like this:

HttpResponse<Car[]> mockedResponse = Mockito.mock(HttpResponse.class);
Mockito.when(mockedResponse.getCode()).thenReturn(200);
Mockito.when(mockedResponse.getBody()).thenReturn(someSerialisedFormOfYourCarArray);

Mockito.when(carsUser.getCars(anyString(), anyString())).thenReturn(mockedResponse);