Spting Boot Testing
September 11, 2021

M04 Q39 What does @SpringBootTest auto-configure?

@SpringBootTest annotation will auto-configure:

  • ApplicationContext for testing
  • Test itself with tools used for testing

ApplicationContext is configured by searching for @SpringBootApplication or @SpringBootConfiguration annotated classes, based on those bean definitions will be created.

It is also possible to test only slice of the application with usage one of following:

  • @SpringBootTest#classes
  • @ContextConfiguration#classes
  • @AutoConfigureā€¦ annotations

@AutoConfigureā€¦ annotations allows you to configure specific environment and tools for testing, for example @AutoConfigureMockMvc will configure Mock Mvc that can be used for Controllers testing.

Spring Boot Test includes annotations that are wrapping @AutoConfigureā€¦ annotations and make test development simpler:

  • @JsonTest - tests serialization and deserialization of the specific object
  • @WebMvcTest - tests one specific controller
  • @WebFluxTest
  • @DataJpaTest - tests interactions with JPA
  • @JdbcTest - tests interactions with JDBC
  • @JooqTest - tests interactions with Jooq
  • @DataMongoTest - tests interactions with Mongo
  • @DataLdapTest
  • @RestClientTest

Each of this annotation uses @AutoConfigureā€¦ annotations and also @ExtendWith(SpringExtension.class) for JUnit 5, which makes test development easier.