Sunday, May 5, 2024
HomeJavaJUnit 5.9 Helps GraalVM Native Picture

JUnit 5.9 Helps GraalVM Native Picture


JUnit 5.9 resolves varied bugs and introduces quite a lot of new options akin to the power to maintain momentary recordsdata after executing a check. New annotations present the power to both allow or disable particular assessments when operating in a GraalVM Native Picture. XML stories are actually saved within the Open Testing Reporting format.

The @TempDir annotation, launched in JUnit 5.4, permits the automated creation and deletion of recordsdata in unit assessments. The next instance reveals the creation of the file JavaVersions.txt, which is routinely deleted on the finish of the check:


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

    Checklist<String> variations = Arrays.asList("17", "18", "19");
    Information.write(file, variations);

    assertEquals(variations, Information.readAllLines(file));
}

JUnit now permits for specifying the cleanup coverage for the @TempDir annotation by way of the CleanupMode enum constants: ALWAYS, DEFAULT, NEVER and ON_SUCCES. CleanupMode.ALWAYS is the default cleanup coverage and could also be modified to CleanupMode.NEVER when declaring the annotation:


@TempDir(cleanup = CleanupMode.NEVER)

After operating the check, the momentary file continues to be seen in a listing which features a random quantity, akin to:


/tmp/junit8656612856066491205/JavaVersions.txt

Transferring ahead, XML stories will probably be saved within the Open Testing Reporting format, which isn’t associated to any testing framework or programming language. The brand new check outcome format makes it simpler for instrument builders to parse check stories with out having to write down parsers for every check framework with its personal distinctive check outcome format.

The newly launched IterationSelector class permits customers to pick particular iterations for a parameterized check in an IDE. At the moment IDEs, akin to IntelliJ, present a inexperienced arrow which permits to rerun your entire check with all parameters:

An open challenge submitted to YouTrack, the JetBrains challenge tracker, proposes to help the IterationSelector class. This means including an arrow for every worth of the parameterized check with a purpose to run the check with only one parameter as a substitute of operating the check for all parameters.

ConsoleLauncher is a command line Java utility to run the JUnit Platform. The output fashion might now be modified with the argument --single-color which solely shows textual content attributes with out coloration or --color-palette=FILE to pick a file which customizes the ANSI fashion output. The newly launched --list-engines argument shows all accessible check engines.

The brand new failIfNoTests attribute of @Suite fails the check suite when no assessments are discovered.

Extensions that needs to be invoked earlier than the creation of check cases can now use the TestInstancePreConstructCallback interface, which is a counterpart of the already present TestInstancePreDestroyCallback interface.

Enabling or disabling assessments based mostly on the working system was already attainable with the @EnabledOnOs and @DisabledOnOs annotations and now contains help for FreeBSD and OpenBSD:


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

Manufacturing facility strategies utilized by the @MethodSource annotation might now use arguments resolved by ParameterResolver extensions:


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

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

static Stream<Arguments> factoryMethod(Integer reply) {
	return Stream.of(
        arguments(reply + " is the reply to life, the universe, and every little thing"),
        arguments(reply + " can also be the reply 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 both allow or disable, respectively, the annotated check when operating inside a GraalVM Native Picture.

The JUnit 5 documentation supplies an entire overview of the brand new options, bug fixes and deprecations.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments