Spring Boot Testing Spring Applications 5 — Questions and Answers
Question 1: Which Spring Boot feature allows you to override property values specifically for a test class?
- @PropertySource
- @TestPropertySource (Correct answer)
- @Profile('test')
- @ConfigurationProperties
Correct answer: @TestPropertySource
@TestPropertySource lets you specify properties files or inline key-value pairs that override application properties during a specific test.
Question 2: What is the purpose of using WireMock in Spring Boot integration tests?
- To mock Spring beans at the class level
- To stub and verify HTTP calls to external services without hitting real endpoints (Correct answer)
- To generate synthetic load for performance testing
- To replace MockMvc for REST endpoint testing
Correct answer: To stub and verify HTTP calls to external services without hitting real endpoints
WireMock starts a local HTTP stub server that you can configure to return predefined responses, isolating your app from real external HTTP dependencies.
Question 3: What does the @AutoConfigureMockMvc annotation do when used alongside @SpringBootTest?
- Enables MockMvc auto-configuration so it can be injected without @WebMvcTest (Correct answer)
- Starts a real embedded HTTP server for MockMvc tests
- Disables security filters in MockMvc requests
- Creates a standalone MockMvc instance without any beans
Correct answer: Enables MockMvc auto-configuration so it can be injected without @WebMvcTest
@AutoConfigureMockMvc tells Spring Boot to configure and inject a MockMvc instance even when using the full @SpringBootTest context.
Question 4: In a Testcontainers-based Spring Boot test, what is the typical use of @DynamicPropertySource?
- To generate random test data for property fields
- To override Spring properties at runtime with values from a started container (e.g., JDBC URL) (Correct answer)
- To bind system environment variables to @Value fields
- To create dynamic bean definitions in the test context
Correct answer: To override Spring properties at runtime with values from a started container (e.g., JDBC URL)
@DynamicPropertySource lets you register container-specific properties (like the JDBC URL from a PostgreSQL container) into the Spring Environment before the context loads.
Question 5: How can you assert that a Spring component throws a specific exception using JUnit 5?
- @Test(expected=MyException.class)
- assertThrows(MyException.class, () -> component.method())
- try { component.method(); } catch(MyException e) {}
- Both B and C achieve the goal but B is preferred (Correct answer)
Correct answer: Both B and C achieve the goal but B is preferred
assertThrows() is the idiomatic JUnit 5 approach and returns the exception for further assertions, but a try-catch also works technically.
Question 6: What is the correct way to test that a Spring application event was published during a service call?
- Poll the event log table in the database after the call
- Inject ApplicationEventPublisher and verify via @MockBean or an ApplicationListener @Bean in @TestConfiguration (Correct answer)
- Use @EventListener in the test class directly
- Assert on the return value of publishEvent()
Correct answer: Inject ApplicationEventPublisher and verify via @MockBean or an ApplicationListener @Bean in @TestConfiguration
You can replace the ApplicationEventPublisher with a @MockBean and use Mockito.verify(), or register a test listener bean to capture published events.
Question 7: Which test slice annotation should you use to test Spring Batch jobs without loading the full application context?
- @DataJpaTest
- @SpringBatchTest combined with @SpringBootTest(classes=BatchConfig.class) (Correct answer)
- @WebMvcTest
- @BatchTest
Correct answer: @SpringBatchTest combined with @SpringBootTest(classes=BatchConfig.class)
@SpringBatchTest provides Batch-specific test utilities like JobLauncherTestUtils and JobRepositoryTestUtils; combine it with a scoped @SpringBootTest to load only Batch configuration.
Which Spring Boot feature allows you to override property values specifically for a test class?