-
Notifications
You must be signed in to change notification settings - Fork 4
JUnit 5
You can use JUnit 5 and QuickPerf BOM files.
In the case of Maven, you can add the following dependency management:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-bom</artifactId>
<version>1.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
The following JUnit 5 dependencies are required:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</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:
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-junit5</artifactId>
<scope>test</scope>
</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.
A QuickPerf test executes QuickPerf annotations. With JUnit 5, you can transform your tests into QuickPerf tests in two ways.
Add @QuickPerfTest
on the test classes.
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
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:
@ExpectSelect(2)
@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)
.
π Β Core
π Β JVM
π Β SQL
π Β Scopes
π Β Create an annotation
π Β JUnit 4
π Β JUnit 5
π Β TestNG
π Β Spring
π Β Detect and fix N+1 SELECT
π Β Maven performance
π Β Spring Boot - JUnit 4
π Β Spring Boot - JUnit 5
π Β Micronaut Data - JUnit 5
π Β Micronaut - Spring - JUnit 5
π Β Quarkus - JUnit 5
π Β FAQ
π Β QuickPerf code