Question

I'm trying to test-driven build a simple admin app. The basic idea is to send data (application/json) between client and server. Therefore I want to test my spring controller using EasyMock. This would be my controller methods with my given path /user:

@RequestMapping(method = RequestMethod.GET, 
          consumes = "application/json", produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public List<User> getUsers() { 
  return null; 
}

@RequestMapping( value = "/create", method = RequestMethod.GET, 
          consumes = "application/json", produces = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public void create(@PathVariable User user) { }

Now I want to test these two methods. So i mocked my service layer "andReturn" my "expect", which is for getUsers() simply a list of users or an empty list. But now I'm not sure how to correctly test my response status.

Was it helpful?

Solution

there is a solution for testing spring mvc controllers and is much more convenient than manually mocking everything. MockMvc class is designed for this.

Please take a look at: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-normal-controllers/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top