Broadcom conceptsSpring Professional DevelopSpring testing
2V0-72.22 Testing: what the Spring exam tests
What the Spring Professional Develop exam asks about Testing: TestContext defaults, rollback, @SpringBootTest environments, test slices and MockMvc setups.
3 min read
By default, the TestContext framework rolls back a transaction for each test. Yet a @Transactional test on a real port does not roll back what the server did, because client and server run in separate threads.
01 What does the Spring Professional Develop exam test about Testing?
You finish a mock, and the testing questions felt easy until the results came back. Sort your notes into two areas and learn the defaults in each.
Two areas of the Testing domain and what to know in each
- TestContext integration testsContext reuse, injection into tests and transaction rollback.
- Boot and web tests@SpringBootTest environments, test slices and MockMvc setups.
Spring integration tests need no application server or other deployment environment.
02 What does the TestContext framework do by default?
Start with the framework underneath. Once loaded, a test ApplicationContext is reused for each test, so the setup cost is paid once per test suite.
Each test also runs in a transaction that is rolled back by default, through a PlatformTransactionManager bean in the test's context. Add @Commit when a test should commit instead.
To wire it into JUnit Jupiter, @SpringJUnitConfig combines @ExtendWith(SpringExtension.class) with @ContextConfiguration.
Rollback is the default for test transactions, and @Commit switches it off.
03 How do @SpringBootTest and test slices load the context?
Next, add Spring Boot. With JUnit 6, you do not add @ExtendWith(SpringExtension.class) because @SpringBootTest already carries it.
Two @SpringBootTest web environments compared
MOCK (default)
- Web ApplicationContext
- Mock web environment
- No embedded server started
DEFINED_PORT
- WebServerApplicationContext
- Real web environment
- Port from application.properties or 8080
Using several @…Test slice annotations in one test is not supported; pick one and add the other slices' @AutoConfigure… annotations by hand.
A nested @TestConfiguration adds to your primary configuration, while a nested @Configuration replaces it.
04 When should a MockMvc test load the whole web context?
Finally, the controllers. MockMvc tests Spring MVC without a running server by invoking the DispatcherServlet with mock Servlet API objects.
A WebApplicationContext setup loads your real Spring MVC configuration, while standalone mode tests one controller with mocks injected by hand and loads no Spring configuration.
Standalone MockMvc tests need extra integration tests to verify your Spring MVC configuration.
Summary
- A loaded test context is reused, and test transactions roll back by default.
- One @…Test slice annotation per test; add other slices by hand.
- Standalone MockMvc skips Spring configuration, so add integration tests.
Sources
- Integration Testing :: Spring Framework (opens in a new tab)
- Testing Spring Boot Applications :: Spring Boot (opens in a new tab)
- Setup Options :: Spring Framework (opens in a new tab)
- Spring JUnit Jupiter Testing Annotations :: Spring Framework (opens in a new tab)
- Overview :: Spring Framework (opens in a new tab)