What is the equivalent of TestName rule in JUnit 5?

Declare a parameter of type TestInfo in your test method and JUnit will automatically supply an instance of that for the method: @Test void getTestInfo(TestInfo testInfo) { // Automatically injected System.out.println(testInfo.getDisplayName()); System.out.println(testInfo.getTestMethod()); System.out.println(testInfo.getTestClass()); System.out.println(testInfo.getTags()); } You can get test method name (and more) from the TestInfo instance as shown above.

JUnit5 tag-specific gradle task

Based on https://github.com/gradle/gradle/issues/6172#issuecomment-409883128 Amended in 2020 to take lazy task configuration and Gradle 5 into account. See answer’s history for older versions. plugins { id “java” } def test = tasks.named(“test”) { useJUnitPlatform { excludeTags “integration” } } def integrationTest = tasks.register(“integrationTest2”, Test) { useJUnitPlatform { includeTags “integration” } shouldRunAfter test } tasks.named(“check”) { dependsOn … Read more

What is proper workaround for @BeforeAll in Kotlin

JUnit 5 has @TestInstance(PER_CLASS) annotation that can be used for this purpose. One of the features that it enables is non-static BeforeAll and AfterAll methods: @TestInstance(PER_CLASS) class BeforeAllTests { lateinit var isInit = false @BeforeAll fun setup() { isInit = true } @TestFactory fun beforeAll() = listOf( should(“initialize isInit in BeforeAll”) { assertTrue(isInit) } ) … Read more

Generating display names for @ParameterizedTest in JUnit 5

As of JUnit 5.8.0, there is a Named<T> interface as part of the JUnit Jupiter API with “automatic support for injecting the contained payload [the arguments] into parameterized methods directly” (see issue #2301). Example: @DisplayName(“A parameterized test with named arguments”) @ParameterizedTest @MethodSource(“namedArguments”) void testWithNamedArguments(File file) {} static Stream<Arguments> namedArguments() { return Stream.of( Arguments.of(Named.of(“An important file”, … Read more

junit5 MethodSource in nested class

@TestInstance(PER_CLASS) You may select the “single test instance per class” mode annotating the nested class with @TestInstance(TestInstance.Lifecycle.PER_CLASS): class ColorTest { @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) class Inner { @ParameterizedTest @MethodSource(“colors”) void blue(Color color, int blue) { Assertions.assertEquals(color.getBlue(), blue); } Stream<Arguments> colors() { return Stream.of( Arguments.of(Color.BLACK, 0), Arguments.of(Color.GRAY, 128), Arguments.of(Color.BLUE, 255) ); } } } When using this mode, … Read more

Difference between junit-jupiter-api and junit-jupiter-engine

junit-jupiter aggregator artifact JUnit 5.4 provides much simpler Maven configuration if your intent is to write JUnit 5 tests. Simply specify the aggregate artifact named junit-jupiter. <!– https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter –> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.1</version> <scope>test</scope> </dependency> As an aggregate, this artifact in turn pulls the following three artifacts automatically, for your convenience: junit-jupiter-api (a compile dependency) … Read more

Maven not running JUnit 5 tests

According to the annotation (import org.junit.jupiter.api.Test), you are trying to run JUnit 5 tests with Maven. According to the documentation, you have to add this dependency: <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.3.1</version> <scope>test</scope> </dependency> Your version of Maven comes with a version of maven-surefire-plugin which does not support JUnit 5. You could update your Maven to the … Read more