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