Spring boot – @SpringBootTest

Learn about @SpringBootTest annotation provided by Spring boot to enable boot specific features in the application tests during unit testing or integration testing.

1. @SpringBootTest

We can specify @SpringBootTest annotation on a test class that runs Spring Boot based tests. It’s class declaration is as below:

public @interface SpringBootTest 

It provides the following features over and above the regular Spring TestContext provided by @ContextConfiguration(classes=…​) annotation in spring-test.

  • Automatically searches for a @SpringBootConfiguration when nested @Configuration class is not used, and no explicit classes are specified.
  • Allows custom environment properties to be defined using the properties attribute.
  • Provides support for different webEnvironment modes, including the ability to start a fully running web server listening on a defined or random port.
  • Registers a TestRestTemplate and/or WebTestClient bean for use in web tests that are using a fully running web server.

2. Using @SpringBootTest for integration testing

Under the hood, @SpringBootTest tries to mimic the processes added by Spring Boot framework for creating the context e.g. it decides what to scan based on package structures, loads external configurations from predefined locations, optionally runs auto-configuration starters and so on.

As we see that this annotation starts and configure almost whole application before the test begin, we should use @SpringBootTest to write an integration tests that use the application processes and dependencies.

public class SpringBootDemoApplicationTests 
    int randomServerPort;

    //---- tests -----

3. Using @SpringBootTest for unit testing

@SpringBootTest annotation loads whole application, but it is better to limit Application Context only to a set of spring components that participate in test scenario.

3.1. ‘classes’ attribute

The classes attribute specifies the annotated classes to use for loading an ApplicationContext.

@SpringBootTest(classes = {EmployeeRepository.class, EmployeeService.class})
public class SpringBootDemoApplicationTests 
    private EmployeeService employeeService;
    //---- tests -----

3.2. *…Test annotations

If we are writing unit tests then it’s always better now to use @SpringBootTest annotation. Rather use the specialized spring boot test annotations which test a very specific slice of the application.

These annotations disable full auto-configuration and instead apply only configuration relevant to specific layers of the codebase.

Some of these anntations are given below:

  • @WebFluxTest – can be used in combination with @RunWith(SpringRunner.class) for a typical Spring WebFlux test which focuses only on Spring WebFlux components. It will also auto-configure a WebTestClient.
  • @JdbcTest – can be used when a test focuses only on jdbc-based components. The annotation configures an in-memory embedded database and JdbcTemplate. By default, tests are transactional and roll back at the end of each test.
  • @DataMongoTest – can be used when a test focuses only on MongoDB components. By default, tests annotated with @DataMongoTest will use an embedded in-memory MongoDB process if the driver is available through dependencies. It also configures a MongoTemplate, scans for @Document classes, and configures Spring Data MongoDB repositories.
  • @DataRedisTest – can be used to test Redis applications. It scans for @RedisHash classes and configures Spring Data Redis repositories.
  • @DataLdapTest – can be used to test LDAP applications. It configures an in-memory embedded LDAP (if available), configures a LdapTemplate, scans for @Entry classes, and configures Spring Data LDAP repositories.
  • @RestClientTest – can be used to test REST clients. It auto-configures dependencies like Jackson, GSON, and Jsonb support, configures a RestTemplateBuilder, and adds support for MockRestServiceServer by default.

  • @JooqTest – be used when a test focuses only on jOOQ-based components.

4. Conclusion

As discussed above, use @SpringBootTest annotation for writing the integration tests in spring boot application – because ,by default, it scans and loads the whole application context and all spring boot related auto-configurations.

If we are targeting to write unit tests and want to test few specific sections of the application, we shall use the specialized ‘*…Test’ annotations as discussed later in the tutorial.

Drop me your questions related to how to do integration testing in spring boot in comments.

Happy Learning !!

Ref : @SpringBootTest Spring Docs


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.