Are you tired of writing tests which have a lot of boilerplate code? If so, get started with Spock Framework >>

Introduction to Spring MVC Test Framework

This blog post is the first part of my new Spring MVC Test tutorial. This tutorial helps you to write unit and integration tests for Spring MVC controllers with JUnit 5. However, before we can get to the good stuff, we have to understand the basics.

After we have finished this blog post, 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.

Let’s begin

What Is the Spring MVC Test Framework?

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

  • We can write our tests by using JUnit 4, JUnit 5, or TestNG. We can also use other testing frameworks as long as the testing framework provides support for the Spring MVC Test framework or we use the standalone configuration.
  • Because the Spring MVC Test framework is build on the top of the Servlet API mock objects provided by the spring-test module, it doesn’t require a servlet container.
  • It uses the DispatcherServlet class that provides full support for the Spring MVC runtime behavior.
  • We can configure the system under test by using the standalone configuration or the web application context based configuration. We will talk more about these configuration options in the next posts of this tutorial.

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

The key components of the Spring MVC Test framework are:

  • The MockMvc class acts as an entry point for our unit and integration tests. To more specific, we will use this class when we send HTTP requests to the system under test.
  • The MockMvcBuilders class provides static factory methods which allow us to create new MockMvc objects.
  • The MockMvcRequestBuilders class provides static factory methods which we can use for creating the HTTP requests send to the system under test.
  • The MockMvcResultMatchers class provides static factory methods which allow us to write assertions for the returned HTTP response.
  • The MockMvcResultHandlers class provides static factory methods which we can use when we want to print the returned HTTP response or write the HTTP response to a log file by using the Apache Commons Logging library.

Let’s move on and find out how we can get the required dependencies with Maven and Gradle.

Getting the Required Dependencies

This section describes how we can get the required dependencies when we declare our dependencies one by one and when we use the dependency management of Spring Boot.

Declaring Our Dependencies One by One

If we want to declare our dependencies one by one, 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 to the dependencies section of our pom.xml file:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
    <version>5.1.9.RELEASE</version>
</dependency>

If we are using Gradle, we have to add the spring-test dependency to the testImplementation dependency configuration.

If we are using the Groovy DSL, we have to add the following snippet to the dependencies block of our build.gradle file:

testImplementation('org.springframework:spring-test:5.1.9.RELEASE')

If we are using the Kotlin DSL, we have to add the following snippet to the dependencies block of our build.gradle.kts file:

testImplementation("org.springframework:spring-test:5.1.9.RELEASE")

Using the Dependency Management of Spring Boot

If we want to use the dependency management of Spring Boot, we have to declare the spring-boot-starter-test dependency in our build script.

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

If we are using Gradle, we have to add the spring-boot-starter-test dependency to the testImplementation dependency configuration.

If we are using the Groovy DSL, we have to add the following snippet to the dependencies block of our build.gradle file:

testImplementation('org.springframework.boot:spring-boot-starter-test')

If we are using the Kotlin DSL, we have to add the following snippet to the dependencies block of our build.gradle.kts file:

testImplementation("org.springframework.boot:spring-boot-starter-test")
Naturally, a real software project requires other testing dependencies as well. If you want to get more information about the required dependencies, you should take a look at the example applications of my new Spring MVC Test tutorial (a work in progress).

Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us four 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.
  • If we want to declare our dependencies one by one, we have to declare the spring-test dependency in our build script.
  • If we want to use the dependency management of Spring Boot, we have to declare spring-boot-starter-test dependency in our build script.
0 comments… add one

Leave a Comment