spring-boot
junit5
testing
spring-boot-starter-test
software-development

spring-boot-starter-test with JUnit 5

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

spring-boot-starter-test is the standard test starter for Spring Boot projects and works naturally with JUnit 5. It gives you a practical baseline for unit tests, slice tests, and integration tests without making you assemble every dependency by hand. The important part is not just adding the starter, but using the right level of Spring support for each test so the suite stays fast and trustworthy.

Add the Starter Correctly

For Maven:

xml
1<dependency>
2  <groupId>org.springframework.boot</groupId>
3  <artifactId>spring-boot-starter-test</artifactId>
4  <scope>test</scope>
5</dependency>

For Gradle:

groovy
testImplementation 'org.springframework.boot:spring-boot-starter-test'

That starter normally brings in JUnit Jupiter, assertions, mocking support, Spring test utilities, and useful Boot test annotations. In modern Spring Boot projects, JUnit 5 is the normal path, so you should not need JUnit 4 unless you are maintaining older tests.

Keep Plain Unit Tests Plain

Most business logic does not need a Spring context. A fast unit test should just use JUnit 5 directly.

java
1import org.junit.jupiter.api.Test;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4
5class TaxServiceTest {
6
7    @Test
8    void calculatesTax() {
9        double subtotal = 100.0;
10        double total = subtotal * 1.13;
11        assertEquals(113.0, total);
12    }
13}

This style is fast, isolated, and ideal for the majority of pure logic tests.

Use @SpringBootTest Only When You Need Full Wiring

@SpringBootTest loads the full application context. That is valuable when you want to verify wiring, configuration, or startup behavior, but it is expensive compared with plain unit tests.

java
1import org.junit.jupiter.api.Test;
2import org.springframework.boot.test.context.SpringBootTest;
3
4@SpringBootTest
5class ApplicationContextTest {
6
7    @Test
8    void contextLoads() {
9    }
10}

Use this sparingly. If every test uses @SpringBootTest, your suite becomes slow and noisy very quickly.

Prefer Slice Tests for Focused Coverage

Spring Boot includes test slices that load only one part of the framework. That gives you better feedback speed without giving up framework integration entirely.

A controller example with @WebMvcTest:

java
1import org.junit.jupiter.api.Test;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
4import org.springframework.test.web.servlet.MockMvc;
5
6import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
7import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
8
9@WebMvcTest(HealthController.class)
10class HealthControllerTest {
11
12    @Autowired
13    MockMvc mockMvc;
14
15    @Test
16    void returnsOk() throws Exception {
17        mockMvc.perform(get("/health"))
18                .andExpect(status().isOk());
19    }
20}

Other useful slices include @DataJpaTest, @JsonTest, and @RestClientTest.

Mock Collaborators Deliberately

When a slice test needs one dependency replaced, use @MockBean.

java
1import org.springframework.boot.test.mock.mockito.MockBean;
2
3@WebMvcTest(UserController.class)
4class UserControllerTest {
5
6    @MockBean
7    UserService userService;
8}

That is useful, but too much mocking can hide real integration problems. Mock boundaries intentionally, not reflexively.

Use Test Profiles and Isolated Configuration

Tests should not depend on a developer laptop's local configuration. A dedicated test profile helps keep the environment deterministic.

java
1import org.springframework.test.context.ActiveProfiles;
2
3@ActiveProfiles("test")
4class RepositoryTest {
5}

This becomes especially important when your application has multiple data sources, custom security settings, or environment-specific beans.

Use JUnit 5 Features Well

JUnit 5 adds features that improve test readability and coverage. Parameterized tests are especially useful.

java
1import org.junit.jupiter.params.ParameterizedTest;
2import org.junit.jupiter.params.provider.CsvSource;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6class MathServiceTest {
7
8    @ParameterizedTest
9    @CsvSource({"2,3,5", "10,5,15"})
10    void addCases(int a, int b, int expected) {
11        assertEquals(expected, a + b);
12    }
13}

This reduces duplication while still keeping the test intent clear.

Common Pitfalls

  • Loading the full Spring context for simple unit tests.
  • Mixing JUnit 4 and JUnit 5 styles without a migration plan.
  • Using @MockBean everywhere and losing confidence in real wiring.
  • Letting tests depend on local services or machine-specific configuration.
  • Ignoring test-runtime growth until CI feedback becomes too slow.

Summary

  • 'spring-boot-starter-test is the standard Spring Boot test starter and works naturally with JUnit 5.'
  • Keep most tests context-free and fast.
  • Use @SpringBootTest only for full integration checks.
  • Prefer slice tests such as @WebMvcTest for focused Spring coverage.
  • Use JUnit 5 features to improve readability and input coverage without bloating the suite.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.