पाठ 2 / 25
JUnit Platform, Jupiter and Build Setup
Set up JUnit with Maven or Gradle and understand the JUnit architecture.
How the pieces fit
Modern JUnit is split into modules. The JUnit Platform launches tests and provides the API that build tools and IDEs use. JUnit Jupiter is the programming model you write tests with (@Test, @BeforeEach, assertions, extensions), introduced in JUnit 5 and continued in JUnit 6 (released in 2025, requiring Java 17) with the same package names. JUnit Vintage runs old JUnit 3 and 4 tests on the platform during migrations. Add the junit-jupiter dependency (ideally through the junit-bom to keep versions aligned) with test scope. Maven Surefire (3.x) and Gradle (useJUnitPlatform()) run tests on the platform automatically. Tests live in src/test/java, mirroring the package of the class under test, and test classes conventionally end in Test. Add Mockito (mockito-core and mockito-junit-jupiter) and an assertion library such as AssertJ. Spring Boot projects get all of these through spring-boot-starter-test.
Maven and Gradle test dependencies
Use a BOM so all JUnit modules share one version.
<!-- pom.xml (versions are examples; use the latest stable releases) -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.12.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Gradle (Kotlin DSL):
dependencies {
testImplementation(platform("org.junit:junit-bom:5.12.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.mockito:mockito-junit-jupiter:5.17.0")
}
tasks.test { useJUnitPlatform() }
-->A stage, a script and an old script reader
The Platform is the theatre stage that any play can use, Jupiter is the modern script format, and Vintage is a reader who can still perform old scripts while you rewrite them.
त्वरित जाँच: Which JUnit module provides the annotations and assertions you write modern tests with?
- JUnit Platform
- JUnit Vintage
- Maven Surefire
- JUnit Jupiter
Answer
JUnit Jupiter — Jupiter is the programming and extension model; the Platform launches tests.