Mockito for Spring – book review

This post is a review about the Mockito for Spring book, available here.

 Mockito for Spring book

This book is intended for Java developers with a little background in Spring and software testing. Less experienced developers will also find it valuable as it explains general concepts of unit testing and provides a good introduction to the Spring framework. It is focused on practical examples and as usual with PacktPub, some examples of code are delivered with the book, available online.

The book itself is a great resource for global learning, from Spring ecosystem to common skills like tests practices (TDD).

Spring ecosystem

In the first chapter, the author introduces Spring and its ecosystem (IOC, DI, AOP, JDBC, Transactions, MVC).

JUnit

The second one is focused on the JUnit test framework (unit testing, advanced JUnit practices, tests suites, @RunWith annotation) and Mockito matchers. This chapter provides some good implementations examples to get started with Matchers.

Spring Test framework

The third chapter is quite interesting: it begins with a great dive into Spring Test framework: you will first learn how the Spring Test context works, then how to test your Spring applications with this framework.

To resume, this chapter demonstrates how to test Spring MVC or how to mock a servlet container. It also explains transactions in Spring tests and Spring profiles.

Mockito

The real introduction of Mockito comes in the fourth chapter: you will learn how to unit test each layer of a common application.

The following techniques are explained:

  • Mocking/stubbing
  • Spying/verifying

Spring 4: new features

Finally, the last chapter provides an overview of the new features in Spring 4 (RestController, AsyncTasks, new support for cache).

In this section, you will learn how to work with async tasks with the simple @Async Spring annotation. You will also learn how to build a simple RESTful controller with the @RestController annotation (in Spring 3: @Controller + @ResponseBody), and how to cache objects.

Conclusion

This book provides a great introduction to the Spring Framework and covers advanced subjects like Spring Test Context and Mockito. It is a must read when it comes to testing your Spring applications. The examples in the book are very useful for each subject. They provide a good practical overview.

Here is a link to the book: https://www.packtpub.com/application-development/mockito-spring.