If you are working with Spring MVC, I can guarantee that the answer to this question is yes. Automated tests saves you time, money and a lot of headache. And yet, writing comprehensive tests for Spring MVC controllers has been a complex task. The problem is that if a task is complex, time consuming and it is not absolutely necessary, you are less likely to finish it. That is how we humans work.
I have spend countless hours of writing tests to Spring MVC applications and the results were still mediocre at best. I knew that important areas of my applications were left untested but that was a compromise I had to make. If you have tried writing tests to Spring MVC applications, I am sure that you know what I am talking about here.
Let’s move on and find out how you can write both unit and integration tests for Spring MVC 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.
What Happens Next?
Congratulations. You have now taken the first and very important step of a long journey. Your next step is to put the lessons learned into action. Don’t just think about doing it when you have time to do it. Do it right now.
This section showcases useful material created by other developers. If you have written a blog post or recorded a video about Spring MVC Test (or testing in general), and you want to include it to this section, ping me on Twitter and I will check it out. If I think that it is useful, I will add it to this section.
Spring Test Framework
Reference Manuals and Official Guides
- Spring Framework Reference Manual: 10. Testing
- Introducing Spring Test MVC HtmlUnit
- Spring MVC Test with HtmlUnit
- Spring MVC Test with WebDriver
- Spring MVC Test with Geb
- Getting Started With Spring’s MVC Test Framework – Part 1
- Getting Started With Spring’s MVC Test Framework – Part 2
- Spring Test Context Caching + AspectJ @Transactional + Ehcache pain
Other Testing Tools
- AssertJ provides a rich set of fluent assertions for both unit and integration tests.
- JUnit is the most popular testing framework for Java programming language.
- catch-exception is a library which catches exceptions and makes them available for further analysis.
- DbUnit is a JUnit extension which is used to initialize the database into a known state before integration tests are run. It is also to used to write assertions for the data which is stored to a relational database.
- Hamcrest is a library which provides matchers for both unit and integration tests.
- HtmlUnit is a headless browser for Java. It is often used for testing purposes but you can also use it for retrieving information from web pages.
- JMeter is a popular performance testing tool.
- Mockito is a very popular mocking framework.
- NoSQLUnit is a JUnit extension which is used to initialize a NoSQL database into a known state before our tests are run. It is also to used to write assertions for the data which is stored to a NoSQL database.
- Spring Test DbUnit integrates the Spring Test framework with DbUnit.
- Spring Test MVC HtmlUnit integrates the Spring Test MVC framework with HtmlUnit.
- REST-Assured is a testing framework for REST APIs.
Reference Manuals and Official Guides
- JUnit API
- Getting Started with DbUnit
- DbUnit API
- The Hamcrest Tutorial
- Hamcrest API
- Getting Started with HtmlUnit
- HtmlUnit API
- JMeter User’s Manual
- Mockito Reference Manual
- Mockito API
- Spring Test DbUnit Reference Manual
- Spring Test HtmlUnit Reference Manual
- REST-Assured Reference Manual
- REST-Assured API
- Java Unit Testing
- Unit Testing with JUnit
- JUnit 4 with Hamcrest
- Create Your Own Hamcrest Matcher
- Unit Tests with Mockito
- Mockito When / Then Cookbook
- Mockito Verify Cookbook
- DZone Mockito Cheat Sheet
- Real World JMeter: 00 Intro
- Real World JMeter: 01 How to run JMeter tests with Maven
- Real World Jmeter: 02 Same test, multiple environments
- Real world JMeter: 03 running tests with Jenkins
- Unit Testing Spring MVC Controllers with REST Assured