JUnit 5.9 supports native GraalVM image

JUnit 5.9 fixes various bugs and introduces a number of new features, such as the ability to keep temporary files after running a test. New annotations provide the ability to enable or disable specific tests when running in a native GraalVM image. XML reports are now saved in Open Testing Reporting format.

That @TempDir Note introduced in JUnit 5.4, allows automated creation and deletion of files in unit tests. The following example shows the creation of the file JavaVersions.txtwhich is automatically deleted at the end of the test:

@Test
public void tmpDir(@TempDir Path tempDir) throws IOException {
    Path file = tempDir.resolve("JavaVersions.txt");

    List versions = Arrays.asList("17", "18", "19");
    Files.write(file, versions);

    assertEquals(versions, Files.readAllLines(file));
}

JUnit now allows specifying the cleanup policy for the @TempDir note about the CleanupMode Enum constants: ALWAYS, DEFAULT, NEVER and ON_SUCCES. CleanupMode.ALWAYS is the default purge policy and can be modified CleanupMode.NEVER when declaring the annotation:

@TempDir(cleanup = CleanupMode.NEVER)

After running the test, the temporary file is still visible in a directory containing a random number, such as:

/tmp/junit8656612856066491205/JavaVersions.txt

In the future, XML reports will be stored in Open the test report generator Format not related to a testing framework or programming language. The new test result format makes it easier for tool developers to parse test reports without having to write parsers for each test framework with its own unique test result format.

The newly introduced IterationSelector class allows users to select specific iterations for a parameterized test in an IDE. Currently, IDEs like IntelliJ show a green arrow that allows to run the whole test again with all the parameters:

A open topic submitted to YouTrack, JetBrains’ issue tracker, suggests supporting it IterationSelector Class. This suggests adding an arrow for each value of the parameterized test to run the test with only one parameter instead of running the test for all parameters.

ConsoleLauncher is a Java command line application for running the JUnit platform. The output style can now be changed with the argument --single-color which only displays text attributes with no color or --color-palette=FILE to select a file that customizes the ANSI-style output. The newly introduced --list-engines Argument shows all available test engines.

The new failIfNoTests attribute of @Suite the test-suite fails if no tests are found.

extensions which should be called before creating test instances can now use the TestInstancePreConstructCallback Interface that is a counterpart to the already existing ones TestInstancePreDestroyCallback Interface.

Enabling or disabling tests based on the operating system was already possible with the @EnabledOnOs and @DisabledOnOs Annotations and contains now support for FreeBSD and OpenBSD:

@Test
@EnabledOnOs({OS.FREEBSD, OS.OPENBSD})

Factory methods used by the @MethodSource Annotation can now use arguments resolved by ParameterResolver Extensions:

@RegisterExtension
static final AnswerResolver answerResolver = new AnswerResolver();

@ParameterizedTest
@MethodSource("factoryMethod")
void testAnswers(String answer) {
	assertTrue(answer.startsWith("42"));
}

static Stream factoryMethod(Integer answer) {
	return Stream.of(
        arguments(answer + " is the answer to life, the universe, and everything"),
        arguments(answer + " is also the answer to this questions")
	);
}

static class AnswerResolver implements ParameterResolver {

	@Override
	public boolean supportsParameter(ParameterContext parameterContext, 
            ExtensionContext extensionContext) {
    	return parameterContext.getParameter().getType() == Integer.class;
	}

	@Override
	public Object resolveParameter(ParameterContext parameterContext, 
            ExtensionContext extensionContext) {
    	return 42;
	}

}

JUnit 5.9.1 introduces the @EnabledInNativeImage and @DisabledInNativeImage Annotations to enable/disable the annotated test when run within a Native GraalVM image.

The JUnit 5 documentation provides a complete overview of the new features, bugfixes and deprecations.

Comments are closed.