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

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay