jeudi 29 octobre 2015

Resttemplate unit testing

i am trying to unit test a method which is calling a resttemplate inside. But somehow my test is not working. The method that i want to test looks so

public Integer createAccount(Request request) {
    final String uri = "http://localhost:8080/stubs/otl/account/create";
    return restTemplate.postForObject(uri, request, Integer.class);
}

My Unit tests looks so

@InjectMocks
AccountServiceImpl accountService;

@Mock
RestTemplate restTemplate;

@Value("${url.otl}")
private String urlOtl = "http://localhost:8080/stubs/otl/";

@Before
public void setUp(){
    MockitoAnnotations.initMocks(this);
}

@Test
public void createAccountTest(){

    MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

    mockServer.expect(requestTo(urlOtl+"account/create"))
            .andExpect(method(HttpMethod.POST))
            .andRespond(withSuccess("1", MediaType.APPLICATION_JSON));

    Request request = new Request();
    Integer result = accountService.createAccount(request);
    mockServer.verify();
    Assert.assertEquals("1", String.valueOf(result));
}

When i ran the test i get as response java.lang.AssertionError: Further request(s) expected 0 out of 1 were executed at org.springframework.test.web.client.MockRestServiceServer.verify(MockRestServiceServer.java:167)

Can somebody tell me what i am missing or doing wrong.

Aucun commentaire:

Enregistrer un commentaire