Skip to content

cronn/test-utils

Repository files navigation

CI Valid Gradle Wrapper Maven Central Apache 2.0

cronn test-utils

Utility classes for JUnit 5 tests.

JUnit5MisusageCheck

JUnit5 @BeforeAll, @BeforeEach, @Test, @AfterEach and @AfterAll require that annotated methods are not overriden/hidden, otherwise they are silently ignored. JUnit5MisusageCheck is a simple way to detect such situations and fail fast rather than sweep it under the carpet.

Preferred way to use this extension is to automatically register it for all tests:

  • add META-INF/services/org.junit.jupiter.api.extension.Extension file containing de.cronn.testutils.JUnit5MisusageCheck
  • add junit.jupiter.extensions.autodetection.enabled=true property (for example to junit-platform.properties file)

TestInfoExtension

The same as TestInfo but as extension which you can register as test class field.

H2Util

H2Util allows to restore empty state of H2 database in integration tests. Intended usage in @BeforeEach callback:

@BeforeEach
void cleanupDatabase(@Autowired H2Util h2Util) {
        h2Util.resetDatabase();
}

Empty state is defined as:

  • empty tables, sequences restarted for resetDatabase()
  • empty database schema for dropAllObjects()

Warnings:

  • H2Util requires JPA, Spring and H2 dependencies which are defined as optional in library: you have to provide them on your own.
  • Hibernate can cache sequence values in advance. You might want to disable that by setting hibernate.id.optimizer.pooled.preferred to NONE (for example by adding spring.jpa.properties.hibernate.id.optimizer.pooled.preferred=NONE spring property)

TestClock

TestClock is an implementation of the abstract class Clock, and provides features useful for testing. If not specified it is initialized with a fixed Instant in the past. Its time is fixed, this clock will not run. Therefore, it also provides features to manipulate its time.

ResetClockExtension

Convenient extension if you are using TestClock in Spring based integration tests: it automatically resets TestClock if such bean is found within test ApplicationContext.

Warning: requires Spring dependency which is defined as optional in library: you have to provide it on your own.

ConcurrentTest

ConcurrentTest offers support for tests which need to run on mutliple threads. It handles the overhead of submitting and evaluating each of a given number of a task. It uses the ExecutorServiceUtils to ensure that the executorService is properly shut down and the task queue is cleared, so that subsequent tests can run without interference.

Usage

Add the following Maven dependency to your project:

<dependency>
    <groupId>de.cronn</groupId>
    <artifactId>test-utils</artifactId>
    <version>{version}</version>
    <scope>test</scope>
</dependency>