DEV Community

Salad Lam
Salad Lam

Posted on

Inside Spring Boot test framework

Following is a basic description of Spring test framework for helping to read source code.

org.springframework.test.context.TestExecutionListener interface

Allows to add actions during different phases of testing. Following is some useful classes.

Class Function
MockitoTestExecutionListener setup Mockito mock class
TransactionalTestExecutionListener handle database transaction
ServletTestExecutionListener build necessary Servlet instances when testing Servlet component
DirtiesContextBeforeModesTestExecutionListener and DirtiesContextTestExecutionListener force to recreate ApplicationContext on different phase of testing
WithSecurityContextTestExecutionListener setup SecurityContextHolder
DependencyInjectionTestExecutionListener inject beans into test instance

org.springframework.test.context.TestContextManager class

Contains test class information and it runs methods of TestExecutionListener.

org.springframework.test.context.junit.jupiter.SpringExtension class

Bridge class between JUnit 5 and Spring test framework.

org.springframework.test.context.ContextCustomizer interface

Provides a way to alter ApplicationContext. Following is some useful classes.

  • DynamicPropertiesContextCustomizer
  • MockitoContextCustomizer
  • SpringBootTestWebEnvironment
  • ImportsContextCustomizer
  • ExcludeFilterContextCustomizer
  • TypeExcludeFiltersContextCustomizer
  • SpringBootTestArgs
  • DisableAutoConfigurationContextCustomizer
  • PropertyMappingContextCustomizer

org.springframework.boot.autoconfigure.ImportAutoConfiguration annotation

Spring Boot annotation that to include specific Spring Boot AutoConfiguration class. There are two ways to use it.

@ImportAutoConfiguration(AutoConfigureDatabase.class)
Enter fullscreen mode Exit fullscreen mode

This means that to import AutoConfigureDatabase class.

// ...
@ImportAutoConfiguration
public @interface AutoConfigureTestDatabase {
  // ...
Enter fullscreen mode Exit fullscreen mode

This means that to import entries of AutoConfigureTestDatabase in /META-INF/spring.factories file.

This annotation is used without @EnableAutoConfiguration or with @OverrideAutoConfiguration(enabled = false) when @EnableAutoConfiguration is already annotated in application.

org.springframework.boot.test.autoconfigure.json.JsonTest annotation

Instead of loading a full set of AutoConfiguration classes, a set of AutoConfiguration related to Json testing is imported. Beans defined in your application will be filtered. Following are also imported.

  • anything in class annotated with @SpringBootConfiguration
  • @TestConfiguration classes
  • ContextCustomizer classes
  • TypeExcludeFilter and TestTypeExcludeFilter classes

Other test annotation

  • @SpringBootTest
  • @DataCassandraTest
  • @DataJdbcTest
  • @DataLdapTest
  • @DataMongoTest
  • @DataNeo4jTest
  • @DataR2dbcTest
  • @DataRedisTest
  • @JdbcTest
  • @JooqTest
  • @JsonTest
  • @DataJpaTest
  • @RestClientTest
  • @WebFluxTest
  • @WebMvcTest
  • @WebServiceClientTest
  • @WebServiceServerTest

Top comments (0)