How to use @SpringBootTest in Spring Boot Testing
Use
@SpringBootTest on a test class to load the full Spring application context for integration testing. It allows your tests to run with all Spring beans and configurations loaded, simulating the real application environment.Syntax
The @SpringBootTest annotation is placed on a test class to tell Spring Boot to start the full application context for testing. You can customize it with properties like webEnvironment to control the embedded server behavior.
@SpringBootTest: Loads the full Spring Boot context.webEnvironment: Defines if and how a web environment is started (e.g., MOCK, RANDOM_PORT).- Used with JUnit 5 or 4 test frameworks.
java
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MyIntegrationTest { // test methods }
Example
This example shows a simple integration test using @SpringBootTest that loads the application context and tests a service bean.
java
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.beans.factory.annotation.Autowired; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest public class GreetingServiceTest { @Autowired private GreetingService greetingService; @Test void testGreeting() { String greeting = greetingService.greet("Alice"); assertThat(greeting).isEqualTo("Hello, Alice!"); } } // Service class import org.springframework.stereotype.Service; @Service public class GreetingService { public String greet(String name) { return "Hello, " + name + "!"; } }
Output
Test passed: greetingService returns expected greeting string.
Common Pitfalls
Common mistakes when using @SpringBootTest include:
- Not using
@Autowiredto inject beans, causingnullreferences. - Forgetting to include
@SpringBootTeston the test class, so the context is not loaded. - Using
@SpringBootTestfor simple unit tests, which slows down tests unnecessarily. - Not specifying
webEnvironmentwhen testing web layers, leading to port conflicts or missing server.
Example of a wrong and right way:
java
// Wrong: No @SpringBootTest, bean is null public class WrongTest { @Autowired private GreetingService greetingService; @Test void test() { // greetingService is null here } } // Right: With @SpringBootTest @SpringBootTest public class RightTest { @Autowired private GreetingService greetingService; @Test void test() { // greetingService is injected } }
Quick Reference
- @SpringBootTest: Loads full Spring Boot context for integration tests.
- webEnvironment: Controls embedded server (NONE, MOCK, RANDOM_PORT, DEFINED_PORT).
- @Autowired: Inject beans into test classes.
- Use for integration, not unit tests.
Key Takeaways
Use @SpringBootTest on your test class to load the full Spring Boot application context.
Inject beans with @Autowired to access real components in your tests.
Specify webEnvironment to control embedded server behavior during tests.
Avoid using @SpringBootTest for simple unit tests to keep tests fast.
Always ensure @SpringBootTest is present to avoid null bean injection errors.