问题
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