unable to mock the resttemplate call using restclienttest

余生颓废 提交于 2020-04-30 06:56:05

问题


i want to mock my RestTemplate which uses RestTemplateBuilder. hence i am using restclienttest.

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.

instead of resulting the custom json string "SD", it is performing actual GET call and giving the response from backend.

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?


回答1:


Try to put @Mock annotation on top of RestTemplate and other respective service classes.

@Mock


来源:https://stackoverflow.com/questions/61237686/unable-to-mock-the-resttemplate-call-using-restclienttest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!