Sub topic 2
What spring-boot-starter-test gives you
This chapter assumes you can build a small Spring Boot application with Gradle or Maven and that you have written a JUnit test before. It covers the one dependency that supplies most of your testing tools, and how to check that it works.
One dependency, many libraries
Spring Boot groups its test libraries into a single starter, spring-boot-starter-test. You add it once, and your build gets the following.
| Library | What it does in your tests |
|---|---|
| JUnit Jupiter (JUnit 5) | Test annotations such as `@Test`, plus the engine that runs them |
| AssertJ | Fluent assertions such as `assertThat(x).isEqualTo(y)` |
| Mockito | Creates mock objects and stubs their behavior |
| Spring Test and Spring Boot Test | `MockMvc`, the test context framework, and annotations such as `@SpringBootTest` |
| JSONassert and JsonPath | Compare and query JSON content |
| Hamcrest | Matchers, used by `MockMvc` result checks |
The starter picks versions that work together, so you don't declare or align them yourself. This is the same version management Spring Boot gives you for its runtime dependencies.
Adding it to Gradle
Add the starter with the testImplementation configuration. Test libraries then stay out of your production classpath and your packaged application.
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}The useJUnitPlatform() call is required. Without it, Gradle does not use the JUnit 5 engine, and your @Test methods are silently skipped. In Maven, the equivalent dependency uses <scope>test</scope>, and the Surefire plugin finds JUnit 5 on its own.
If you generated your project with Spring Initializr, the starter is already in the build file.
How the pieces fit together
Each library has a separate job. JUnit runs the test, Mockito replaces collaborators, and AssertJ checks the outcome. Spring's own test support sits on top and loads parts of your application when a test needs them.
The diagram below shows the layers.
A plain unit test uses only the first three. The fourth is needed once a test asks Spring to build a context, which later chapters cover.
Verifying the setup
A quick test confirms that the engine and assertions are wired correctly. It needs no Spring context.
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
class SetupSmokeTest {
@Test
void testLibrariesAreOnTheClasspath() {
assertThat("spring-boot-starter-test").contains("test");
}
}Run ./gradlew test. The report should list one executed test. If it lists zero, check for useJUnitPlatform().
Common errors
- Zero tests run in Gradle.
useJUnitPlatform()is missing. @Testdoes not resolve, or tests run as JUnit 4. You importedorg.junit.Testinstead oforg.junit.jupiter.api.Test.- Test classes not found on the production classpath. Test code belongs under
src/test/java, notsrc/main/java.
Exercise
Your Gradle build has the starter as a testImplementation dependency, but ./gradlew test reports that no tests ran, even though src/test/java contains classes with @Test methods from org.junit.jupiter.api. What is the likely cause, and how do you fix it?
Show answer
The test task is not configured to use the JUnit Platform. Add tasks.named('test') { useJUnitPlatform() } to the build file.