创建 http 响应作为 mockito 的 return 值
Creating http response as a return value for mockito
我是 Mockito 测试的新手,正在测试 GET 请求。
我已经模拟了调用,但现在我需要 return HttpResponse<Cars[]>
因为这些值稍后会在 for 循环中使用。
如何创建一个虚拟 HttpResponse
,其中主体包含 Car
个对象的数组。
classCarProvider
中使用Get请求的方法是:
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);
}
这在另一个 class CarsUsers
下面的方法中使用:
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
}
我的测试方法如下:
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
}
}
我得到 NullPointerException
指向 for(Cars car: response.getBody){}
。
我假设 body 没有任何值,因此抛出异常。
感谢任何帮助或建议。
谢谢
你真的需要 CarProvider
到 return 一个 HttpResponse<Car[]>
吗?
感觉底层通信 (HttpResponse
) 的一个方面可能在这里泄露了。如果 CarProvider
的目的是 提供汽车 那么也许应该相应地输入它。
因此,如果您声明 CarProvider
如下 ...
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[]
}
}
...那么您的测试方法将简化为:
@Test
public void getCarsTest(){
Mockito.when(carsUser.getCars(anyString(), anyString())).thenReturn(getDummyCarsList());
// ...
}
private Cars[] getDummyCarsList(){
return new Car{} {new Car(...), new Car(...)};
}
然而,如果这真的不可能并且你真的必须模拟HttpResponse<Car[]>
那么它看起来像这样:
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);
我是 Mockito 测试的新手,正在测试 GET 请求。
我已经模拟了调用,但现在我需要 return HttpResponse<Cars[]>
因为这些值稍后会在 for 循环中使用。
如何创建一个虚拟 HttpResponse
,其中主体包含 Car
个对象的数组。
classCarProvider
中使用Get请求的方法是:
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);
}
这在另一个 class CarsUsers
下面的方法中使用:
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
}
我的测试方法如下:
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
}
}
我得到 NullPointerException
指向 for(Cars car: response.getBody){}
。
我假设 body 没有任何值,因此抛出异常。
感谢任何帮助或建议。
谢谢
你真的需要 CarProvider
到 return 一个 HttpResponse<Car[]>
吗?
感觉底层通信 (HttpResponse
) 的一个方面可能在这里泄露了。如果 CarProvider
的目的是 提供汽车 那么也许应该相应地输入它。
因此,如果您声明 CarProvider
如下 ...
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[]
}
}
...那么您的测试方法将简化为:
@Test
public void getCarsTest(){
Mockito.when(carsUser.getCars(anyString(), anyString())).thenReturn(getDummyCarsList());
// ...
}
private Cars[] getDummyCarsList(){
return new Car{} {new Car(...), new Car(...)};
}
然而,如果这真的不可能并且你真的必须模拟HttpResponse<Car[]>
那么它看起来像这样:
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);