问题描述
@RunWith(MockitoJUnitRunner.class)
public class FeatureFlipperManagerTest {
@Autowired
RestTemplate restTemplate = new RestTemplate();
@Autowired
Service service = new Service();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
@Test
public void test() throws Exception {
mockServer.expect(requestTo(Mockito.anyString()))
.andRespond(withSuccess("{\"enabled\":true}", MediaType.APPLICATION_JSON));
boolean res = service.isEnabled("xxx");
mockServer.verify();
Assert.assertEquals(true, res);
}
}
我有MockRestServiceServer来模拟服务中的restTemplete.但是它总是失败.它显示错误为java.lang.AssertionError: Further request(s) expected0 out of 1 were executed
.任何人都可以让我知道我做错了什么地方.
I have MockRestServiceServer to mock restTemplete in a service. But it always fail. it shows the error as java.lang.AssertionError: Further request(s) expected0 out of 1 were executed
. Any one could let me know where I did not do it right.
服务本身将如下所示:
public class Service{
public boolean isEnabled(String xxx) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity("someurl",String.class);
if(...)return true;
return false;
}
}
推荐答案
首先,您的Service
类在每个请求上创建一个RestTemplate的新实例.我不能足够强调这种不良做法.创建一个类型为RestTemplate的bean,并将其注入到您的Service
bean中(很可能已经创建-取决于您使用的Spring MVC版本).
First of all, your Service
class creates a new instance of RestTemplate on every request. I cannot stress enough how bad practice it is. Create a bean of type RestTemplate and inject it into your Service
bean (it is most likely already created - depending on the Spring MVC version you are using).
一旦有了它,那么这两个RestTemplates:一个在Service
bean中,另一个在FeatureFlipperManagerTest
中注入,并且可以用MockRestServiceServer
进行测试.
Once you have it, then both RestTemplates: one in your Service
bean and one injected into FeatureFlipperManagerTest
will be the same and testing with MockRestServiceServer
will be possible.
编辑-更明确地说:
将您的Service
类修改为:
@Component
public class Service {
private RestTemplate restTemplate;
@Autowired
public Service(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public boolean isEnabled(String xxx) {
ResponseEntity<String> response = restTemplate.getForEntity("someurl",String.class);
if(...)return true;
return false;
}
}
和您的测试班级:
@RunWith(MockitoJUnitRunner.class)
public class FeatureFlipperManagerTest {
@Autowired
RestTemplate restTemplate;
@Autowired
Service service;
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
@Test
public void test() throws Exception {
mockServer.expect(requestTo(Mockito.anyString()))
.andRespond(withSuccess("{\"enabled\":true}", MediaType.APPLICATION_JSON));
boolean res = service.isEnabled("xxx");
mockServer.verify();
Assert.assertEquals(true, res);
}
}
如果此操作失败,并指出没有RestTemplate
bean,则请粘贴有关您正在使用的Spring版本的信息(Spring Boot?).
If this fails with exception saying that there is no RestTemplate
bean present then please paste info about version of Spring (Spring Boot?) you are using.
这篇关于如何使用MockRestServiceServer模拟RestTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!