This tutorial helps you to write comprehensive unit and integration tests for Spring MVC controllers. After you have finished this tutorial, you can write unit and integration tests for both “normal” web application and REST APIs.
My New Spring MVC Test Tutorial
I started to write a new version of my Spring MVC Test tutorial. When this tutorial is finished, it describes how you can write both unit and integration tests for Spring MVC controllers by using the Spring MVC Test framework and JUnit 5. At the moment, my new Spring MVC Test tutorial consists of these blog posts:
- Introduction to Spring MVC Test framework provides a quick introduction to Spring MVC Test framework. After you have read this blog post, you can identify the key components of the Spring MVC Test framework, and get the required dependencies with Maven and Gradle.
- The Best Way to Configure the Spring MVC Test Framework, Part One helps you to select the best way to configure the Spring MVC Test framework when you are writing unit tests.
- Writing Unit Tests for “Normal” Spring MVC Controllers: Configuration describes how you can configure the system under test by using the standalone configuration when you are writing unit tests for controller methods that render data or process form submissions.
- Writing Tests for Spring MVC Controllers: Test Case 101 provides a very quick introduction to sending HTTP requests to the system under test when you are using the Spring MVC Test framework.
Archive
This section contains links to my old blog posts which are more or less outdated. However, I decided to keep these links on this page because they might be useful to people who are working with legacy applications.
Introducing: Unit Testing of Spring MVC Controllers
This three-part tutorial describes how you can write unit tests for Spring MVC controllers. This tutorial consists of the following blog posts:
- Unit Testing of Spring MVC Controllers: Configuration describes how you can configure unit tests which use the Spring MVC Test framework.
- Unit Testing of Spring MVC Controllers: “Normal” Controllers talks about the unit testing of “normal” controllers.
- Unit Testing of Spring MVC Controllers: REST API describes how you can write unit tests for a REST API.
If you prefer watching videos over reading blog posts, you can watch the video version of this tutorial.
- Screencast: Unit Testing of Spring MVC Controllers – Configuration
- Screencast: Unit Testing of Spring MVC Controllers – “Normal” Controllers
- Screencast: Unit Testing of Spring MVC Controllers – REST API
Introducing: Integration Testing of Spring MVC Applications
This eight-part tutorial describes how you can write integration tests for Spring MVC applications. The parts of this tutorial are described in the following:
- Integration Testing of Spring MVC Applications: Configuration describes how you can configure your test cases. If you are writing tests for an application which uses Spring Framework 3.2, you should read the last part of this tutorial as well.
- Integration Testing of Spring MVC Applications: Controllers teaches you to initialize the used database by using Spring-Test-DBUnit, to verify the state of database after the test and to write integration tests to “normal” Spring MVC controllers.
- Integration Testing of Spring MVC Applications: Forms describes how you can write integration tests to form controllers. This part teaches you to send your form objects in the body of the request and describes how you can ensure that the validation rules of your form are working correctly.
- Integration Testing of Spring MVC Applications: REST API, Part One teaches you to write integration tests for a REST API which reads information from the database. You learn to verify the content type of the response, to verify the character set of the response and to write assertions against the response body.
- Integration Testing of Spring MVC Applications: REST API, Part Two describes how you can write integration tests for a REST API which stores information to the database. You learn how you can set the content type of the request and convert objects to JSON which is send in the body of the request.
- Integration Testing of Spring MVC Applications: Security describes how you can write integration tests which ensure that the security rules of your application are working correctly. You learn to configure your security security tests and to specify the principal which is used in your tests.
- Integration Testing of Spring MVC Applications: Migrating to Spring 3.2 describes how you can migrate your integration tests from Spring Framework 3.1 to Spring Framework 3.2. It describes the required dependencies and the changes you have to make to your tests.
- Integration Testing of Spring MVC Applications: Write Clean Assertions with JsonPath describes how you can write cleaner tests for a REST API by using JsonPath expressions.