Mockito for Spring
上QQ阅读APP看书,第一时间看更新

What this book covers

Chapter 1, Getting Familiar with the Spring Framework, covers the basics of Spring, Spring projects, and especially the Spring Framework. It explores the Spring container, the life cycle of beans, dependency injection, AOP, Spring MVC, and Spring transaction management.

Chapter 2, Working with JUnit and Mockito, covers both basic and advanced JUnit usages. It covers annotation-based JUnit testing, assertion, the @RunWith annotation, exception handling, and the Eclipse setup to run JUnit tests, matchers, and assertThat, as well as the custom lessThanOrEqual() matcher. The Working with Mockito section explores the Mockito framework and provides technical examples to demonstrate the capability of Mockito.

Chapter 3, Working with Spring Tests, illustrates every aspect of unit testing your Spring applications. It starts with TestContext and explores the JUnit 4 enabled SpringJUnit4ClassRunner. Then, it explores Spring profiles that can be used to work with the different sets of configuration files as well as the Spring environment and how to mock the environment with MockEnvironment and MockPropertySource. We use the ReflectionTestUtils method to access the private fields of the Spring beans. The chapter provides usage examples of Spring annotations for testing, unit tests the MVC application with MockHttpServletRequest, MockHttpSession, and ModelAndViewAssert, and mocks the servlet container with MockMvc to handle actual requests and responses, as they will be at runtime. You will also perform real Spring integration and transaction management with annotations such as @Transactional, @TransactionConfiguration, and @Rollback.

Chapter 4, Resolving Out-of-container Dependencies with Mockito, deals with unit testing the service layer in isolation from the data access layer with Mockito, unit testing the Spring data access layer with Mockito, and unit testing the Spring presentation layer (MVC) with Mockito.

Chapter 5, Time Travelling with Spring, starts by covering the features of the new major Spring release 4.0, such as Java 8 support and so on. Then, we pick the four Spring 4 topics and explore them one by one. The Working with asynchronous tasks section showcases the execution of long running methods asynchronously and provides examples to handle asynchronous processing. The Exploring @RestController section eases RESTful web service development with the advent of the @RestController annotation. The Learning AsyncRestTemplate section explains the RESTful client code to invoke RESTful web services asynchronously. Caching is inevitable for high performant, scalable web applications. This section explains EhCache and Spring integration to achieve a high availability caching solution.