【发布时间】:2019-03-30 02:45:23
【问题描述】:
我有球衣客户喜欢
public ClientResponse getCall(String apiName){
restClient = getRestClient();
WebResource webResource = restClient
.resource(Constants.FUELWISE_END_POINT+apiName);
return webResource.accept(Constants.APPLICATION_JSON)
.header(Constants.AUTHORIZATION_HEADER, Constants.AUTHORIZATION_VALUE).get(ClientResponse.class);
}
并有类似的模拟测试用例行:
@Test
public void getCallTest() {
when(restClient.resource(any(String.class))).thenReturn(webResource);
when(webResource.accept(any(String.class)).header(any(String.class), any(Object.class))
.get(eq(ClientResponse.class))).thenReturn(clientResponse);
restUtilityTest.getCall("messages");
}
已扩展测试 java 类以从方法 getRestClient() 返回 restClient 模拟而不是 Client.create()
使用注解使用@Mock 进行模拟
堆栈跟踪错误:
This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
在测试用例文件中完成模拟:
@InjectMocks
private RestUtilityTest restUtilityTest;
@Mock
private Client restClient;
@Mock
private WebResource webResource;
@Mock(name = "response")
private ClientResponse clientResponse;
【问题讨论】:
-
没有足够的上下文来提供帮助。请使用附加代码更新问题(并格式化代码)。
-
获取休息客户端代码:
-
@divyanayan 你真的应该把edit 这个附加信息放入问题中。不在 cmets 部分。
-
@divyanayan 展示了如何初始化被测对象以及如何注入模拟的依赖项。
标签: java unit-testing mockito