Nicholas
Nicholas

Reputation: 21

Unit test for Spring MVC Controllers that use annotation @RequestParam

How can I create a Unit test for Spring MVC Controllers that use annotation @RequestParam? I have created junit tests for controller that uses HttpServletRequest object in the handlerequest method, but I am looking a way to test a controller using @RequestParam.

Thanks

@RequestMapping("/call.action")

public ModelAndView getDBRecords(@RequestParam("id") String id) {

   Employee employee = service.retrieveEmployee(id);

} 

Upvotes: 2

Views: 4516

Answers (4)

Abhishek Ramachandran
Abhishek Ramachandran

Reputation: 1170

Try it as your test method !

@Test
    public void testgetDBRecords(){
      MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
      mockMvc.perform(get("/call.action?id=id1234").andExpect(status().isOk())
    }

Upvotes: 0

Vova Rozhkov
Vova Rozhkov

Reputation: 1732

Use integration test (google Spring MVC integration testing)

Kinda this

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationContextLoader;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = YourApplication.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class SampleControllerTest {

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

    @Autowired
    protected WebApplicationContext context;

    private RestTemplate restTemplate = new RestTemplate();

    @Test
    public void returnsValueFromDb() {
        // you should run mock db before
        String id = "a0972ca1-0870-42c0-a590-be441dca696f";
        String url = "http://localhost:" + port + "/call.action?id=" + id;

        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());

        String body = response.getBody();

        // your assertions here
    }

}

Upvotes: 0

Carl
Carl

Reputation: 1

Or, you can use _request = new MockHttpServletRequest();

and _request.setAttribute("key", "value");

Upvotes: 0

skaffman
skaffman

Reputation: 403471

One of the charms of this style of controller is that your unit tests don't need to worry about the mechanics of request mapping. They can just test the target code directly, with no mucking about with request and response objects.

So write your unit test as if it were just any other class, and ignore the annotations. In other words, call getDBRecords() from your test and pass in the id argument. Remember, you don't need to unit-test Spring itself, you can assume that works.

There is another class of tests ("functional" or "acceptance" tests) that tests the application once it has been deployed (using e.g. WebDriver, Selenium, HtmlUnit, etc). This is the place to test that your mapping annotations are doing the job.

Upvotes: 11

Related Questions