Consider a test class annotated with @SpringBootTest. What is the main effect of this annotation when running the test?
Think about whether the whole app context is started or just parts of it.
@SpringBootTest tells Spring Boot to start the entire application context, allowing integration tests to run with all beans loaded as in a real app.
Look at these options for @SpringBootTest(webEnvironment = ...). Which one starts the embedded server on a random port?
Random port means the server starts on any free port to avoid conflicts.
WebEnvironment.RANDOM_PORT starts the embedded server on a random free port, useful for real HTTP integration tests.
Given this test class:
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@Test
void testService() {
assertNotNull(myService);
}
}The test fails because myService is null. What is the likely cause?
Check if the bean is properly registered in the Spring context.
If MyService is not annotated with @Service or @Component, Spring will not create a bean for it, so injection fails.
Consider this test class:
@SpringBootTest(properties = {"app.feature.enabled=false"})
public class FeatureTest {
@Value("${app.feature.enabled}")
private boolean featureEnabled;
@Test
void testFeatureFlag() {
System.out.println(featureEnabled);
}
}What will be printed when the test runs?
Properties in @SpringBootTest override application properties.
The properties attribute sets app.feature.enabled to false, so featureEnabled is false.
Which reason best explains why @SpringBootTest is preferred over @WebMvcTest for full integration testing?
Think about how much of the app is loaded and tested.
@SpringBootTest loads the full app context, allowing tests to cover all layers including database and services, unlike @WebMvcTest which only loads web layer beans.