无法使用restclienttest模拟resttemplate调用 [英] unable to mock the resttemplate call using restclienttest
问题描述
我想模拟使用RestTemplateBuilder的RestTemplate.因此,我正在使用restclienttest.
i want to mock my RestTemplate which uses RestTemplateBuilder. hence i am using restclienttest.
不幸的是,我无法模拟resttemplate调用. 当调用Sup supExpected = myService.getDetails("1234")时.它执行了完整的后端调用,而不是导致我要执行的操作.
unfortunately i am not able to mock the resttemplate call. when Sup supExpected = myService.getDetails("1234"), is called. its performing the complete backend call instead resulting what i have asked to do.
它不是执行自定义json字符串"SD",而是执行实际的GET调用并给出来自后端的响应.
instead of resulting the custom json string "SD", it is performing actual GET call and giving the response from backend.
这是我的jnuit代码:
here is my jnuit code:
@RunWith(SpringRunner.class)
@RestClientTest(MyService.class)
public class TestMyServiceApplication {
@Autowired
private MyService myService;
@Autowired
private RestTemplate restTemplate;
@Autowired
private MockRestServiceServer server;
@Before public void setUp() {
server= MockRestServiceServer.createServer(restTemplate); }
@Test
public void ReturnSupplierSuccessfully() {
String SD= "{\"SUP\": {\"LNR\": \"1234\",\"NAME1\": \"RestClient\"}]}}";
this.server
.expect(requestTo("abc.net/service/v1?lnr=1234"))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(SD, MediaType.APPLICATION_JSON));
Sup supExpected = myService.getDetails("1234");
server.verify();
assertNotNull(supExpected);
}
有人可以帮助我我的代码有什么问题吗?
can anyone help me what's wrong in my code?
推荐答案
尝试将@Mock
注释放在RestTemplate和其他各个服务类之上.
Try to put @Mock
annotation on top of RestTemplate and other respective service classes.
@Mock
这篇关于无法使用restclienttest模拟resttemplate调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!