Lesson 13 · Testing · Section 4

Testing Spring Applications

JUnit 5, full-context integration tests, the fast test slices, MockMvc, and @MockBean.

What you'll be tested on That @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.

JUnit 5 + Spring

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.

Full integration tests — @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:

webEnvironmentBehaviourTest with
MOCK defaultMock servlet environment, no real serverMockMvc
RANDOM_PORTReal embedded server on a random free portTestRestTemplate / WebTestClient
DEFINED_PORTReal server on the configured portTestRestTemplate
NONENo 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.

Test slices — load only what you need

Booting the whole context is slow. A slice annotation auto-configures just one layer and scans only the relevant beans:

SliceLoadsDoes NOT load
@WebMvcTestControllers, @ControllerAdvice, converters, filters — and auto-configures MockMvc@Service/@Repository/@Component beans (mock them with @MockBean)
@DataJpaTestEntities, repositories, TestEntityManager; in-memory DB; @Transactional (rolls back)The web layer & services
@JsonTestJSON serialisation (Jackson) onlyEverything else

MockMvc and @MockBean

@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());
  }
}
Databases & profiles in tests @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.
What's in 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.)
Primary source — read this
Spring Boot 2.5 — Testing & Spring Framework 5.3 — Testing

Read "Testing Spring Boot Applications", "@SpringBootTest", "Auto-configured Tests (slices)", and "Mocking and Spying Beans".

Check yourself

The slice behaviours, @MockBean vs @Mock, and the webEnvironment options are the money questions. Options shuffle on every load.

Second pass The same ground is in your book's Boot Testing and Testing sections. Then continue — Security is next.
I'm your teacher — ask me anything. Want a full @WebMvcTest + @MockBean example, or the difference between @DataJpaTest and a full @SpringBootTest for repository tests? Ask. Say "continue" for Lesson 14 — Spring Security.
← Lesson 12 · REST Lesson 14 · Spring Security → (coming next)