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.
- The Spring MVC Test framework doesn't send real HTTP requests to the system under test. All tests are run in a mock environment provided by Spring MVC Test framework.
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 providesstatic
factory methods which allow us to create newMockMvc
objects. - The
MockMvcRequestBuilders
class providesstatic
factory methods which we can use for creating the HTTP requests send to the system under test. - The
MockMvcResultMatchers
class providesstatic
factory methods which allow us to write assertions for the returned HTTP response. - The
MockMvcResultHandlers
class providesstatic
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.
Additional Reading:
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.3.15</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.3.15')
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.3.15")
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")
Let's summarize what we learned from this blog post.
Summary
This blog post 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.
- The Spring MVC Test framework doesn't send real HTTP requests to the system under test. All tests are run in a mock environment provided by Spring MVC Test framework.
- 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.
Written very simply and clearly. Thanks!
You are welcome!