2

I looked at this link : How to write a unit test for a Spring Boot Controller endpoint

I am planning to unit test my Spring Boot Controller. I have pasted a method from my controller below. When I use the approach mentioned in the link above , will the call that I have to service.verifyAccount(request) not be made? Are we just testing whether the controller accepts the request in format specified and returns response in format specfied apart from testing the HTTP status codes?

@RequestMapping(value ="verifyAccount", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifyAccountResponse> verifyAccount(@RequestBody VerifyAccountRequest request) {

    VerifyAccountResponse response = service.verifyAccount(request);

    return new ResponseEntity<VerifyAccountResponse>(response, HttpStatus.OK);
}
Community
  • 1
  • 1
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • 1
    It depends upon whether you have mocked the service object. If you have not mocked, it would call the service. – notionquest Sep 21 '16 at 18:16
  • Thanks @notionquest . What is the purpose of MockMvc and if I don't use mock objects , will all my dependencies get injected by using the code (accepted answer) in the other post? – Punter Vicky Sep 21 '16 at 18:25
  • Maybe this post http://stackoverflow.com/questions/32223490/are-springs-mockmvc-used-for-unit-testing-or-integration-testing is helpful for answering your "what is the purpose of MockMvc" question. – Michael Lihs Sep 21 '16 at 20:14

1 Answers1

0

You can write unit test case using

@RunWith(SpringJUnit4ClassRunner.class)
    // Your spring configuration class containing the                            
  @EnableAutoConfiguration
    // annotation
    @SpringApplicationConfiguration(classes = Application.class)
    // Makes sure the application starts at a random free port, caches it         throughout
    // all unit tests, and closes it again at the end.
    @IntegrationTest("server.port:0")
    @WebAppConfiguration

make sure you configure all your server configuration like port/url

 @Value("${local.server.port}")
 private int port;


private String getBaseUrl() {
    return "http://localhost:" + port + "/";
}

Then use code mentioned below

        protected <T> ResponseEntity<T>  getResponseEntity(final String       
  requestMappingUrl, final Class<T> serviceReturnTypeClass, final Map<String, ?>       
  parametersInOrderOfAppearance) {
    // Make a rest template do do the service call
    final TestRestTemplate restTemplate = new TestRestTemplate();
    // Add correct headers, none for this example

    final HttpEntity<String> requestEntity = new HttpEntity<String>(new       
  HttpHeaders());
    // Do a call the the url
    final ResponseEntity<T> entity = restTemplate.exchange(getBaseUrl() +       
  requestMappingUrl, HttpMethod.GET, requestEntity, serviceReturnTypeClass,       
  parametersInOrderOfAppearance);
    // Return result
    return entity;
    }

@Test
public void getWelcomePage() {
    Map<String, Object> urlVariables = new HashMap<String, Object>();
    ResponseEntity<String> response = getResponseEntity("/index",     
    String.class,urlVariables);

    assertTrue(response.getStatusCode().equals(HttpStatus.OK));
}
Ankur Gupta
  • 301
  • 4
  • 12