JUnit 5 - quick-perf/doc GitHub Wiki

🚩 Table of contents

Dependencies

QuickPerf test

Dynamic Tests

Project examples

Dependencies

⚠️ JUnit 5 >= 5.6.0 is required

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.

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:

@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).

Project examples

JVM & JUnit 5

Hibernate & JUnit 5

Spring Boot & JUnit 5

Spring Boot & JUnit 5 & Testcontainers

Micronaut & Hibernate

Micronaut Data

Quarkus

⚠️ **GitHub.com Fallback** ⚠️