HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot 2 / Spring boot – @SpringBootTest

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:

@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Inherited
@BootstrapWith(value=SpringBootTestContextBootstrapper.class)
@ExtendWith(value=org.springframework.test.context.junit.jupiter.SpringExtension.class)
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.

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class SpringBootDemoApplicationTests 
{   
    @LocalServerPort
    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 
{   
	@Autowired
    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

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Spring Boot Test

  • Spring Boot – Testing Guide
  • Spring Boot – @SpringBootTest
  • Spring Boot – @WebFluxTest
  • Spring Boot – @RestClientTest
  • Spring Boot – @DataJpaTest
  • Spring Boot – @TestConfiguration
  • Spring Boot – @MockBean
  • Spring Boot – MockMvc
  • Spring Boot – MockMvc Async
  • Spring Boot – TestRestTemplate
  • Spring Boot – JUnit
  • Spring Boot – Junit 5
  • Spring Boot – Test Controller
  • Spring Boot – Test Service Layer
  • Spring Boot – Integration Testing

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces