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, it's 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 junit 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?