JUnit Installation Guide
JUnit can be added to your project in multiple ways, including Maven, Gradle, and direct JAR download. Below are detailed instructions for each version.
JUnit 5 Installation
Maven Configuration
Add the following dependencies to your pom.xml:
<!-- JUnit Jupiter Aggregator Module -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.14.3</version>
<scope>test</scope>
</dependency>
If using Maven Surefire Plugin to run tests, add the following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
Gradle Configuration
Add dependency to your build.gradle:
testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3'
To run tests:
test {
useJUnitPlatform()
}
Direct JAR Download
Download required JAR files from Maven Central:
- junit-jupiter-api: JUnit Jupiter API
- junit-jupiter-engine: JUnit Jupiter Engine
- junit-platform-console-standalone: Standalone Console Launcher
JUnit 4 Installation
Maven Configuration
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
Gradle Configuration
testImplementation 'junit:junit:4.13.2'
Direct JAR Download
Download junit-4.13.2.jar from Maven Central:
https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
JUnit Vintage Installation
To run JUnit 4 tests on the JUnit 5 platform, add the Vintage engine dependency:
Maven
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.14.3</version>
<scope>test</scope>
</dependency>
Gradle
testImplementation 'org.junit.vintage:junit-vintage-engine:5.14.3'
Version Compatibility
- JUnit 5 requires Java 8 or higher
- JUnit 4 requires Java 5 or higher
- JUnit Vintage 5.14.3 requires JUnit 4.12 or higher
Verify Installation
Create a test class to verify installation:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SampleTest {
@Test
void testExample() {
assertEquals(2, 1 + 1);
}
}
Run tests:
mvn test
# or
gradle test
