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.
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:
For Gradle:
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.
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.
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:
Other useful slices include @DataJpaTest, @JsonTest, and @RestClientTest.
Mock Collaborators Deliberately
When a slice test needs one dependency replaced, use @MockBean.
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.
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.
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
@MockBeaneverywhere 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-testis the standard Spring Boot test starter and works naturally with JUnit 5.' - Keep most tests context-free and fast.
- Use
@SpringBootTestonly for full integration checks. - Prefer slice tests such as
@WebMvcTestfor focused Spring coverage. - Use JUnit 5 features to improve readability and input coverage without bloating the suite.
Related reading
- spring-boot-starter-tomcat vs spring-boot-starter-web
- Spring-Boot and Kafka How to handle broker not available?
- Spring-boot application-test.properties
- spring-boot application without a datasource
- Spring-boot default profile for integration tests
- Spring-boot default profile for integration tests
- Spring-boot automatically import applicationContext.xml?
- spring-boot default log location

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.