-
Notifications
You must be signed in to change notification settings - Fork 4
JUnit 5
Table of contents
Dependencies
QuickPerf test
Dynamic Tests
Project examples
Dependencies
You can use JUnit 5 and QuickPerf BOM files.
In the case of Maven, you can add the following dependency management:
<dependencies>
<dependency>
<groupId>org.junitgroupId>
<artifactId>junit-bomartifactId>
<version>5.7.0version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.quickperfgroupId>
<artifactId>quick-perf-bomartifactId>
<version>1.1.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
The following JUnit 5 dependencies are required:
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.platformgroupId>
<artifactId>junit-platform-launcherartifactId>
<scope>testscope>
dependency>
If you get a java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherDiscoveryRequestBuilder, then the junit-platform-launcher dependency is missing.
You also need the following QuickPerf dependency:
<groupId>org.quickperfgroupId>
<artifactId>quick-perf-junit5artifactId>
<scope>testscope>
dependency>
You can now use the core and JVM annotations, except the JVM profiling annotations, by transforming the test class into a QuickPerf test class (see just below).
Want to use SQL annotations with Spring? Please read this.
QuickPerf test
A QuickPerf test executes QuickPerf annotations. With JUnit 5, you can transform your tests into QuickPerf tests in two ways.
QuickPerfTest annotation
Add @QuickPerfTest on the test classes.
Automatically register QuickPerf extension
JUnit 5 can automatically register QuickPerf extension. So, with this mechanism, you don't have to add QuickPerfTest annotation on the test methods.
To enable the automatic extension registering, add a junit-platform.properties file in src/test/resources. After that, add the following line to this file:
junit.jupiter.extensions.autodetection.enabled=true
Dynamic Tests
With JUnit 5, you can define a test method that generates other tests. This particular method is a test factory, creating dynamic tests.
You can use Quickperf annotations on test factory methods, be aware that in this case, the annotation applies to all dynamic tests created by the test factory method, not on the test factory method on itself.
The following example will fail:
@TestFactory
public List<DynamicTest> execute_two_select_but_five_select_expected() {
List<DynamicTest> dynamicTests = new ArrayList<>();
dynamicTests.add(DynamicTest.dynamicTest("Dynamic test 1", () -> {
Query query = entityManager.createQuery("FROM " + Book.class.getCanonicalName());
query.getResultList();
}));
dynamicTests.add(DynamicTest.dynamicTest("Dynamic test 2", () -> {
Query query = entityManager.createQuery("FROM " + Person.class.getCanonicalName());
query.getResultList();
}));
return dynamicTests;
}
The @ExpectSelect(2) annotation will apply to both dynamic tests; as each generates only one query, you will have two failures (one for each test).
The correct usage would have been @ExpectSelect(1).
Project examples
Annotations
Core
JVM
SQL
Scopes
Create an annotation
Supported frameworks
How to
Project examples
Maven performance
Spring Boot - JUnit 4
Spring Boot - JUnit 5
Micronaut Data - JUnit 5
Micronaut - Spring - JUnit 5
Quarkus - JUnit 5