Skip to content

Spring Boot testing slices

Spring Boot testing slices are a category of test annotations used to limit the scope of the Spring Boot [[ApplicationContext]] created during integration tests.^[600-developer__spring__springboot.md] Instead of loading the full application context, these "slices" configure only the specific beans and infrastructure required to test a particular layer of the application^[600-developer__spring__springboot.md].

By restricting the context, slice tests typically execute faster and reduce the complexity of test setup and maintenance^[600-developer__spring__springboot.md].

Common Slice Annotations

Spring Boot provides specific annotations for different layers of a standard application:

  • @DataJpaTest: Used for testing JPA (Java Persistence API) components. It focuses on the JPA repository layer and typically configures an embedded database^[600-developer__spring__springboot.md].
  • @DataJdbcTest: Used for testing raw JDBC (Java Database Connectivity) components^[600-developer__spring__springboot.md].
  • @DataElasticsearchTest: Used for testing Elasticsearch repositories and components^[600-developer__spring__springboot.md].

Context Configuration

When a test is annotated with a slice annotation, Spring Boot applies a filtered set of auto-configuration intended only for that specific type of test^[600-developer__spring__springboot.md]. This prevents the entire application stack—such as MVC controllers, security filters, or unrelated data sources—from being loaded when they are not required^[600-developer__spring__springboot.md].

Sources

^[600-developer__spring__springboot.md]