chapter five

5 Testing subsets of an application with test slices

 

This chapter covers

  • Testing a single layer of the application with test slices, and when to use them
  • Using slices in practice, such as @WebMvcTest and @RestClientTest
  • Testing a business domain module with Spring Modulith
  • Extending existing test slices and creating new slices

When testing applications with the @SpringBootTest annotation, as we’ve done extensively in the previous chapters, Spring Boot Tests loads the full application context, everything from controllers to repositories. For big applications, with hundreds or thousands of beans, or with dependencies that have a long startup time like Hibernate, the impact on test suite execution time can be significant. If your Spring application context takes a full minute to start up on your development machine, then launching a single test will take just as long. With context caching, subsequent tests should be much faster, but one extra minute is already too long when you run tests very frequently. And if you need multiple test contexts, the duration will scale linearly with the number of contexts, or worse.

5.1 Test slices: isolating technical components

5.2 When to use test slices

5.3 @WebMvcTest for the web layer

5.4 @RestClientTest for HTTP clients

5.5 Business-logic slices with Modulith

5.5.1 A short introduction to Spring Modulith

5.5.2 Testing with Modulith

5.6 Navigating tradeoffs of data-layer tests

5.7 Going further: understanding how test slices work

5.7.1 Test slice autoconfiguration

5.7.2 Type exclusion (and inclusion)

5.8 Summary