How to change tests execution order in JUnit5?

Edit: JUnit 5.4 is officially released now, so no need to use snapshots anymore. This is now possible with JUnit 5.4. https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order To control the order in which test methods are executed, annotate your test class or test interface with @TestMethodOrder and specify the desired MethodOrderer implementation. You can implement your own custom MethodOrderer or … Read more

How to launch JUnit 5 (Platform) from the command line (without Maven/Gradle)?

Sure, use the ConsoleLauncher. The ConsoleLauncher is a command-line Java application that lets you launch the JUnit Platform from the console. For example, it can be used to run JUnit Vintage and JUnit Jupiter tests and print test execution results to the console. An executable *junit-platform-console-standalone-<version>.jar* with all dependencies included is published in the central … 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

How do you organize tests in a modular Java project?

Welcome to Testing In The Modular World! Which kind of tests do you want write? Extra-module tests: Create a test-only project (no “src/main” directory) and declare a “src/test/java/module-info.java” module descriptor. In-module tests: As it was from Day 1 you need to “blend in”/merge/shadow your test classes into your main classes or vice versa. Here you … Read more

How to prepare a nested data structure for a data-driven test in Karate?

I don’t recommend nesting unless absolutely necessary. You may be able to “flatten” your permutations into a single table, something like this: https://github.com/intuit/karate/issues/661#issue-402624580 That said, look out for the alternate option to Examples: which just might work for your case: https://github.com/intuit/karate#data-driven-features EDIT: In version 1.3.0, a new @setup life cycle was introduced that changes the … Read more

Gradle 5 JUnit BOM and Spring Boot Incorrect Versions

How do I disable JUnit coming from the Gradle Spring Dependency Management plugin? For starters, if you are using the dependency management plugin from Spring, you should not be importing the junit-bom since that results in duplicate (and potentially conflicting) management of those dependencies. Aside from that, whenever you use the dependency management plugin from … Read more

In JUnit 5, how to run code before all tests

This is now possible in JUnit5 by creating a custom Extension, from which you can register a shutdown hook on the root test-context. Your extension would look like this; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL; public class YourExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource { private static boolean started = false; @Override public void beforeAll(ExtensionContext context) { … Read more