JUnit-installatiegids

JUnit kan op meerdere manieren aan uw project worden toegevoegd, waaronder via Maven, Gradle en directe JAR-download. Hieronder vindt u gedetailleerde instructies voor elke versie.

JUnit 5 installeren

Maven-configuratie

Voeg de volgende afhankelijkheden toe aan uw 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>

Als u de Maven Surefire Plugin gebruikt om tests uit te voeren, voeg dan de volgende configuratie toe:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.5.2</version>
</plugin>

Gradle-configuratie

Voeg de afhankelijkheid toe aan uw build.gradle:

testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3'

Om tests uit te voeren:

test {
    useJUnitPlatform()
}

Directe JAR-download

Download de benodigde JAR-bestanden van Maven Central:

  • junit-jupiter-api: JUnit Jupiter API
  • junit-jupiter-engine: JUnit Jupiter Engine
  • junit-platform-console-standalone: Zelfstandige console-launcher

JUnit 4 installeren

Maven-configuratie

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

Gradle-configuratie

testImplementation 'junit:junit:4.13.2'

Directe JAR-download

Download junit-4.13.2.jar van Maven Central:

https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar

JUnit Vintage installeren

Om JUnit 4-tests op het JUnit 5-platform uit te voeren, voegt u de Vintage-engine-afhankelijkheid toe:

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'

Versiecompatibiliteit

  • JUnit 5 vereist Java 8 of hoger
  • JUnit 4 vereist Java 5 of hoger
  • JUnit Vintage 5.14.3 vereist JUnit 4.12 of hoger

Installatie verifiëren

Maak een testklasse om de installatie te verifiëren:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class SampleTest {
    @Test
    void testExample() {
        assertEquals(2, 1 + 1);
    }
}

Voer de tests uit:

mvn test
# or
gradle test