Do you want to learn an easy way to write tests for Spring MVC applications?
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.
The Spring MVC Test framework solves these problems. Lets move on and find out how the Spring MVC Test framework helps us to do a better job.
Introducing: Integration Testing of Spring MVC Applications
This eight-part tutorial teaches you everything you need to know about Spring Test MVC (aka spring-test-mvc).
Even though this tutorial concentrates on integration testing of Spring MVC applications, you can easily adapt its lessons to unit testing as well. All you have to do is to replace the service implementation with a mock and you are ready to go.
Also, you can use the lessons learned for writing tests to applications which are using Spring Framework 3.1 or 3.2.
Enough with smalltalk. Each part of this tutorial is 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.
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.
P.S. If you liked my Spring MVC Test tutorial, take a look at my Spring Data book.
