Introduction to Spring MVC Test Framework

After we have finished this lesson, we

  • Know what the Spring MVC Test framework is.
  • Can identify the key components of the Spring MVC Test framework.
  • Know how we can get the required dependencies with Maven and Gradle.

What Is the Spring MVC Test Framework?

Spring MVC Test is a testing framework that provides support for testing Spring MVC controllers. When we want to write unit tests which use the Spring MVC Test framework, we have to know that:

  • It supports both JUnit and TestNG.
  • Because Spring MVC Test is build on the top of Servlet API mock objects provided by the spring-test module, it doesn’t use a servlet container.
  • It uses the DispatcherServlet class that provides full support for Spring MVC runtime behavior.
  • We can configure the system under test by using the standalone configuration or web application context based setup. We will talk more about configuring our unit tests in the next lessons of this topic.

Next, we will take a look at the key components of the Spring MVC Test Framework.

The Key Components of the Spring MVC Test Framework

When we want to configure the Spring MVC Test Framework, we have to use these components:

  • The MockMvc class acts as an entry point that we use for creating the HTTP requests which are processed by the tested controller.
  • The MockMvcBuilders class provides static factory methods that we can use for creating new MockMvc objects.
  • If we want to configure the Spring MVC infrastructure programmatically, we have to use a StandaloneMockMvcBuilder object that is returned by the standaloneSetup() method of the MockMvcBuilders class.
  • If we want to configure the Spring MVC infrastructure by using an application context configuration class or an XML configuration file, we have to use a DefaultMockMvcBuilder object that is returned by the webAppContextSetup() method of the MockMvcBuilders class.

When we want to create an HTTP request by using the created MockMvc object, send it to the tested controller, and write assertions for the returned response, we have to use these components:

  • The MockMvcRequestBuilders class provides static factory methods that we can use for creating the HTTP requests processed by the tested contoller.
  • The methods of the MockMvcRequestBuilders class return MockHttpServletRequestBuilder and MockMultipartHttpServletRequestBuilder objects that help us to configure the “input” that is send to the tested controller.
  • The MockMvcResultMatchers class provides static factory methods that we can use for writing assertions for the returned HTTP response.
  • The MockMvcResultHandlers class provides static factory methods that we can use for printing the returned response or writing the returned response to a log file by using the Apache Commons Logging library.

We will talk more about these components in the next lessons of this topic. Let’s move on and find out how we can get the required dependencies.

Getting the Required Dependencies

This section describes how we can get the required dependencies of a normal Spring web application and a web application that uses Spring Boot.

Getting the Dependencies of a Spring Web Application

If we are writing unit tests for a normal Spring web application, we have to declare the spring-test dependency in our build script.

If we are using Maven, we can declare this dependency by adding the following snippet into our pom.xml file:

1<dependency>
2	<groupId>org.springframework</groupId>
3	<artifactId>spring-test</artifactId>
4	<scope>test</scope>
5	<version>4.2.7.RELEASE</version>
6</dependency>

If we are using Gradle, we can declare this dependency by adding the following snippet into our build.gradle file:

1testCompile(
2	'org.springframework:spring-test:4.2.7.RELEASE'
3)
If we are writing unit tests for a “normal” Spring web application, we have to declare other testing dependencies. Check the required dependencies from the example applications of the other lessons of this topic.

Getting the Dependencies of a Spring Boot Web Application

If we are writing unit tests for a web application that uses Spring Boot, we have to include the spring-boot-starter-test starter in our build script.

If we are using Maven, we have to add the following snippet into our pom.xml file:

1<dependency>
2	<groupId>org.springframework.boot</groupId>
3	<artifactId>spring-boot-starter-test</artifactId>
4</dependency>

If we are using Gradle, we have to add the following snippet into our build.gradle file:

1testCompile(
2	'org.springframework.boot:spring-boot-starter-test'
3)
If we are writing unit tests for a web application that uses Spring Boot, we have to declare other testing dependencies. Check the required dependencies from the example applications of the other lessons of this topic.

Let’s summarize what we learned from this lesson.

Summary

This lesson has taught us five things:

  • The Spring MVC Test framework doesn’t require a servlet container.
  • The Spring MVC Test framework provides full support for Spring MVC runtime behavior.
  • We can identify the key components of the Spring MVC Test framework and we have a general idea when we have to use them.
  • If we are writing unit tests for a normal Spring web application, we have to declare the spring-test dependency in our build script.
  • If we are writing unit tests for a web application that uses Spring Boot, we have to include the spring-boot-starter-test starter in our build script.