Unit Testing Spring MVC REST Controllers

Today’s post is about unit testing Spring MVC REST controllers using MockMvcBuilders standalone setup.

Complete source code for this post can be cloned from github.

Libraries Used

Name Version
Spring MVC 4.0
Spring Test 4.0
Junit 4.8.1
Servlet 3.0
Jackson Mapper 1.9.13
Json Path 0.8.1

pom.xml

Controller Code

Annotation Name Usage
@Controller Marking the class as MVC controller.
@RequestMapping Maps the request URL to a class or method.
@ResponseBody Return type will be written into response body.

Response Model Object

Controller Test Class

test-servlet-context.xml


Since we are interested to get the response in JSON format we are using Jackson Message Converter.

One thought on “Unit Testing Spring MVC REST Controllers

  1. Hi,

    Good tutorial.
    One more thing, there is dependency missing for jackson core.
    Please add this maven dependency.

Leave a comment

Your email address will not be published. Required fields are marked *