JUnit 5, full-context integration tests, the fast test slices, MockMvc, and @MockBean.
@SpringBootTest loads the whole context (and its webEnvironment options);
what each slice (@WebMvcTest, @DataJpaTest) does and doesn't load;
MockMvc for controller tests; and @MockBean vs a plain @Mock.
Spring integrates with JUnit 5 via the SpringExtension. You rarely add it by hand —
@SpringBootTest and the slice annotations are meta-annotated with
@ExtendWith(SpringExtension.class) already. (That's the JUnit 5 replacement for JUnit 4's
@RunWith(SpringRunner.class).)
JUnit 5 lifecycle: @Test, @BeforeEach/@AfterEach (per method),
@BeforeAll/@AfterAll (once), @DisplayName, @Disabled.
@SpringBootTest@SpringBootTest boots the entire ApplicationContext for an integration test (and
the context is cached and reused across tests for speed). Its webEnvironment controls the web layer:
webEnvironment | Behaviour | Test with |
|---|---|---|
MOCK default | Mock servlet environment, no real server | MockMvc |
RANDOM_PORT | Real embedded server on a random free port | TestRestTemplate / WebTestClient |
DEFINED_PORT | Real server on the configured port | TestRestTemplate |
NONE | No web environment at all | — |
RANDOM_PORT starts a genuine server but picks a free port, so it won't clash with other running instances or parallel tests.
Booting the whole context is slow. A slice annotation auto-configures just one layer and scans only the relevant beans:
| Slice | Loads | Does NOT load |
|---|---|---|
@WebMvcTest | Controllers, @ControllerAdvice, converters, filters — and auto-configures MockMvc | @Service/@Repository/@Component beans (mock them with @MockBean) |
@DataJpaTest | Entities, repositories, TestEntityManager; in-memory DB; @Transactional (rolls back) | The web layer & services |
@JsonTest | JSON serialisation (Jackson) only | Everything else |
MockMvc and @MockBeanMockMvc — server-side testing of controllers without starting a real HTTP server. Auto-configured by @WebMvcTest (or add @AutoConfigureMockMvc to a @SpringBootTest).@MockBean — puts a Mockito mock into the ApplicationContext, replacing (or adding) that bean. A plain @Mock is not in the context — that's the key difference for Spring tests.@WebMvcTest(AccountController.class)
class AccountControllerTest {
@Autowired MockMvc mvc;
@MockBean AccountService service; // mock the collaborator in the context
@Test
void getReturns200() throws Exception {
mvc.perform(get("/api/accounts/1"))
.andExpect(status().isOk());
}
}
@Transactional on a test rolls the changes back afterwards (Lesson 9). @Sql runs SQL
scripts before/after a test method. @ActiveProfiles("test") selects profiles; @TestPropertySource
overrides properties.
spring-boot-starter-test
JUnit 5, Spring Test & Spring Boot Test, AssertJ, Hamcrest, Mockito, JSONassert, JsonPath —
all test-scoped, added for you. (Hibernate is not in it — that's the JPA starter.)
Read "Testing Spring Boot Applications", "@SpringBootTest", "Auto-configured Tests (slices)", and "Mocking and Spying Beans".
The slice behaviours, @MockBean vs @Mock, and the webEnvironment options are the money questions. Options shuffle on every load.
@WebMvcTest + @MockBean example, or the difference between
@DataJpaTest and a full @SpringBootTest for repository tests? Ask. Say
"continue" for Lesson 14 — Spring Security.