问题描述
我想使用模拟来测试restTemplate.getForObject
方法,但是有问题.我是Mockito的新手,所以我读了一些有关使用Mockito测试restTemplate的博客,但仍然无法编写成功的测试.要测试的课程是:
I want to test the restTemplate.getForObject
method using a mock but having issues. I'm new with Mockito, so I read some blogs about testing restTemplate using Mockito but still can not write a successful test.The class to test is :
package rest;
@PropertySource("classpath:application.properties")
@Service
public class RestClient {
private String user;
// from application properties
private String password;
private RestTemplate restTemplate;
public Client getClient(final short cd) {
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));
Client client = null;
try {
client = restTemplate.getForObject("http://localhost:8080/clients/findClient?cd={cd}",
Client.class, cd);
} catch (RestClientException e) {
println(e);
}
return client;
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public void setRestTemplate(final RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}
我的测试课:
package test;
@PropertySource("classpath:application.properties")
@RunWith(MockitoJUnitRunner.class)
public class BatchRestClientTest {
@Mock
private RestTemplate restTemplate;
@InjectMocks
private RestClient restClient;
private MockRestServiceServer mockServer;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void getCraProcessTest() {
Client client=new Client();
client.setId((long) 1);
client.setCd((short) 2);
client.setName("aaa");
Mockito
.when(restTemplate.getForObject("http://localhost:8080/clients/findClient?cd={cd},
Client.class, 2))
.thenReturn(client);
Client client2= restClient.getClient((short)2);
assertEquals(client, client2);
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public RestClient getRestClient() {
return restClient;
}
public void setRestClient(RestClient restClient) {
this.restClient = restClient;
}
}
它返回null而不是预期的客户端,Object类的restTemplate
正常工作.我只想写测试.我是否缺少某些东西或做错了测试?感谢您的指导.
It is returning null and not the expected client, the restTemplate
for Object class works fine. I want just to write the test . Am I missing something or doing the test wrong way?Thanks for any guidance.
推荐答案
请改用此:
Mockito.when(restTemplate.getForObject(
"http://localhost:8080/clients/findClient?cd={id}",
Client.class,
new Object[] {(short)2})
).thenReturn(client);
第三个参数是varargs
.因此,您需要在测试中包装为Object[]
,否则Mockito无法匹配它.请注意,这会在您的实现中自动发生.
The third parameter is a varargs
.So you need to wrap into an Object[]
in the test, otherwise Mockito is not able to match it. Note that this happens automatically in your implementation.
也:
-
您在问题示例中忘记了终止
url
(缺少关闭"
).
可能只是错字.
You forgot to terminate your
url
(missing closing"
) in your examples in the question.
Probably just a typo.
您在测试中的实现中使用了不同的url
:...?cd={cd}
而不是...?cd={id}
.
(如@ArnaudClaudel
之前在注释中所指出).
You used different url
's in your implementation in your test: ...?cd={cd}
instead of ...?cd={id}
.
(As previously pointed out by @ArnaudClaudel
in the comments).
您没有为restTemplate.getInterceptors()
定义行为,因此当尝试add
BasicAuthenticationInterceptor
时,我会以NullPointerException
,
失败.
You did not define a behaviour for restTemplate.getInterceptors()
so I would expect it to fail with a NullPointerException
,
when trying to add
the BasicAuthenticationInterceptor
.
此外,您可能想查看我的答案 here
有关如何模拟getForObject
方法的另一个示例.请注意,它不包括任何实际参数为null
的情况.
Additionally you might want to check my answer here
for another example of how to mock the getForObject
method. Note that it does not include a case where any of the real parameters would be null
.
这篇关于如何模拟restTemplate getForObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!