Przewodnik instalacji JUnit
JUnit można dodać do projektu na wiele sposobów, w tym za pomocą Maven, Gradle oraz bezpośredniego pobrania JAR. Poniżej znajdują się szczegółowe instrukcje dla każdej wersji.
Instalacja JUnit 5
Konfiguracja Maven
Dodaj następujące zależności do pliku 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>
Jeśli używasz wtyczki Maven Surefire Plugin do uruchamiania testów, dodaj następującą konfigurację:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
Konfiguracja Gradle
Dodaj zależność do pliku build.gradle:
testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3'
Aby uruchomić testy:
test {
useJUnitPlatform()
}
Bezpośrednie pobranie JAR
Pobierz wymagane pliki JAR z Maven Central:
- junit-jupiter-api: JUnit Jupiter API
- junit-jupiter-engine: JUnit Jupiter Engine
- junit-platform-console-standalone: Samodzielny launcher konsolowy
Instalacja JUnit 4
Konfiguracja Maven
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
Konfiguracja Gradle
testImplementation 'junit:junit:4.13.2'
Bezpośrednie pobranie JAR
Pobierz junit-4.13.2.jar z Maven Central:
https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
Instalacja JUnit Vintage
Aby uruchamiać testy JUnit 4 na platformie JUnit 5, dodaj zależność silnika Vintage:
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'
Kompatybilność wersji
- JUnit 5 wymaga Javy 8 lub nowszej
- JUnit 4 wymaga Javy 5 lub nowszej
- JUnit Vintage 5.14.3 wymaga JUnit 4.12 lub nowszego
Weryfikacja instalacji
Utwórz klasę testową, aby zweryfikować instalację:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SampleTest {
@Test
void testExample() {
assertEquals(2, 1 + 1);
}
}
Uruchom testy:
mvn test
# or
gradle test
