@SpringBootTest helps you run your whole Spring Boot app in a test. It makes sure all parts work together, not just small pieces.
@SpringBootTest for integration tests
@SpringBootTest
public class YourTestClass {
@Test
void testSomething() {
// test code here
}
}This annotation tells Spring Boot to start the full application context for tests.
You can add properties or specify web environment if needed.
@SpringBootTest public class SimpleIntegrationTest { @Test void contextLoads() { // test if app context starts } }
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class WebIntegrationTest { @LocalServerPort private int port; @Test void testHttpCall() { // use port to call REST endpoints } }
@SpringBootTest(properties = {"spring.datasource.url=jdbc:h2:mem:testdb"})
public class DatabaseIntegrationTest {
@Autowired
private UserRepository userRepository;
@Test
void testDatabase() {
// test database access
}
}This test starts the full Spring Boot app on a random port. It uses RestTemplate to call the /greeting endpoint and checks if the response contains "Hello, World".
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class GreetingIntegrationTest { @LocalServerPort private int port; @Autowired private RestTemplate restTemplate; @Test void greetingShouldReturnDefaultMessage() { String url = "http://localhost:" + port + "/greeting"; String response = restTemplate.getForObject(url, String.class); assertThat(response).contains("Hello, World"); } }
Use @SpringBootTest only for integration tests because it starts the whole app and can be slow.
For faster tests of small parts, use @WebMvcTest or @DataJpaTest instead.
Remember to clean up or isolate data when testing with databases.
@SpringBootTest runs your full Spring Boot app for testing.
It helps check if all parts work together correctly.
Use it mainly for integration tests, not small unit tests.
Practice
@SpringBootTest in a Spring Boot project?Solution
Step 1: Understand what
This annotation loads the entire Spring Boot application context, allowing tests to run with all beans and configurations loaded.@SpringBootTestdoesStep 2: Compare with other testing annotations
Unlike unit tests or mocks,@SpringBootTestis used for integration tests that need the full app context.Final Answer:
To load the full application context for integration testing -> Option DQuick Check:
@SpringBootTest = full app context load [OK]
- Thinking it mocks all dependencies automatically
- Confusing it with unit test annotations
- Assuming it only configures database connections
@SpringBootTest to start the web environment on a random port?Solution
Step 1: Recall the correct attribute name and value
The attribute to set the web environment iswebEnvironment, and the enum value for random port isWebEnvironment.RANDOM_PORT.Step 2: Check syntax correctness
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) uses the correct attribute and enum syntax. Other options use incorrect attribute names or string values.Final Answer:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -> Option CQuick Check:
Correct attribute and enum = @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) [OK]
- Using wrong attribute name like webEnv
- Passing string instead of enum
- Trying to set port directly in annotation
\@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class MyIntegrationTest {
\@Autowired
private TestRestTemplate restTemplate;
\@Test
void testHelloEndpoint() {
String response = restTemplate.getForObject("/hello", String.class);
System.out.println(response);
}
}Assuming the
/hello endpoint returns "Hello, Spring!".Solution
Step 1: Understand the test setup
The test uses@SpringBootTestwithRANDOM_PORTand injectsTestRestTemplate, which allows calling the real HTTP endpoint.Step 2: Predict the endpoint call result
The/helloendpoint returns "Hello, Spring!" so the call will return this string and print it.Final Answer:
Hello, Spring! -> Option AQuick Check:
TestRestTemplate calls real endpoint = "Hello, Spring!" [OK]
- Expecting null because of missing mocks
- Assuming 404 because of wrong port
- Thinking context fails to load
NoSuchBeanDefinitionException for TestRestTemplate. What is the likely cause?\@SpringBootTest
class MyTest {
\@Autowired
private TestRestTemplate restTemplate;
\@Test
void test() {
// test code
}
}Solution
Step 1: Identify why TestRestTemplate bean is missing
By default,@SpringBootTestdoes not start the web server unlesswebEnvironmentis set, soTestRestTemplatebean is not created.Step 2: Fix by adding webEnvironment
AddingwebEnvironment = WebEnvironment.RANDOM_PORTorDEFINED_PORTstarts the server and creates the bean.Final Answer:
MissingwebEnvironmentsetting to start web server -> Option BQuick Check:
webEnvironment needed for TestRestTemplate bean [OK]
- Assuming TestRestTemplate is always available
- Forgetting to set webEnvironment in @SpringBootTest
- Thinking @Autowired is missing
@SpringBootTest configuration should you use?Solution
Step 1: Understand webEnvironment options
WebEnvironment.NONEdisables web server startup but loads full context.MOCKuses mock servlet environment,RANDOM_PORTandDEFINED_PORTstart real servers.Step 2: Choose option that disables web server
To speed up tests without web server, useWebEnvironment.NONE.Final Answer:
@SpringBootTest(webEnvironment = WebEnvironment.NONE) -> Option AQuick Check:
Disable web server with WebEnvironment.NONE [OK]
- Using MOCK which still sets up servlet environment
- Starting real server with RANDOM_PORT or DEFINED_PORT
- Confusing NONE with MOCK
Ready to prove it?
Pass the test and this pattern moves unknown
