BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JUnit 5.9 Supports GraalVM Native Image

JUnit 5.9 Supports GraalVM Native Image

Bookmarks

JUnit 5.9 resolves various bugs and introduces a number of new features such as the ability to keep temporary files after executing a test. New annotations provide the ability to either enable or disable specific tests when running in a GraalVM Native Image. XML reports are now stored in the Open Testing Reporting format.

The @TempDir annotation, introduced in JUnit 5.4, allows the automated creation and deletion of files in unit tests. The following example shows the creation of the file JavaVersions.txt, which 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<String> versions = Arrays.asList("17", "18", "19");
    Files.write(file, versions);

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

JUnit now allows for specifying the cleanup policy for the @TempDir annotation via the CleanupMode enum constants: ALWAYS, DEFAULT, NEVER and ON_SUCCES. CleanupMode.ALWAYS is the default cleanup policy and may be changed to CleanupMode.NEVER when declaring the annotation:

@TempDir(cleanup = CleanupMode.NEVER)

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

/tmp/junit8656612856066491205/JavaVersions.txt

Moving forward, XML reports will be stored in the Open Testing Reporting format, which is not related to any 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, such as IntelliJ, show a green arrow which allows to rerun the entire test with all parameters:

An open issue submitted to YouTrack, the JetBrains issue tracker, proposes to support the IterationSelector class. This suggests adding an arrow for each value of the parameterized test in order to run the test with just one parameter instead of running the test for all parameters.

ConsoleLauncher is a command line Java application to run the JUnit Platform. The output style may now be modified with the argument --single-color which only displays text attributes without color or --color-palette=FILE to select a file which customizes the ANSI style output. The newly introduced --list-engines argument displays all available test engines.

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

Extensions that should be invoked before the creation of test instances can now use the TestInstancePreConstructCallback interface, which is a counterpart of the already existing TestInstancePreDestroyCallback interface.

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

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

Factory methods used by the @MethodSource annotation may 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<Arguments> 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 either enable or disable, respectively, the annotated test when running inside a GraalVM Native Image.

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

About the Author

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT