Utility classes for JUnit 5 tests.
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.Extensionfile containingde.cronn.testutils.JUnit5MisusageCheck - add
junit.jupiter.extensions.autodetection.enabled=trueproperty (for example tojunit-platform.propertiesfile)
The same as TestInfo but as extension which you can register as test class field.
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.preferredtoNONE(for example by addingspring.jpa.properties.hibernate.id.optimizer.pooled.preferred=NONEspring property)
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.
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 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.
Add the following Maven dependency to your project:
<dependency>
<groupId>de.cronn</groupId>
<artifactId>test-utils</artifactId>
<version>{version}</version>
<scope>test</scope>
</dependency>