Annotation processor · Java 17+ · Kotlin · Records
Matchers and assertions for your model, generated at compile time.
Name your model classes once. The processor writes a Hamcrest matcher and an AssertJ assertion for each of them, and your production code stays untouched.
assertThat(ADA, isCustomer().withName("Ada"));
CustomerAssert.assertThat(ADA).hasName("Ada");
-
Compile-safe. The matchers and assertions come from your model classes. Rename a property and the test code that uses it stops compiling, instead of failing at run time.
-
Readable failures. A mismatch names the property that differs, nested properties included, with the expected and the actual value.
-
Completeness check. Opt in and a test fails when a property was neither checked nor ignored, so a property added to a model cannot slip by unasserted.
Install
Add the generator for the backend you use, and Hamcrest or AssertJ next to it. The current release is 5.1.0; the snippets read it from the property hamcrest-matcher-generator.version or assertj-assertion-generator.version, which you define once in your build.
<dependency>
<groupId>io.github.marmer.testutils</groupId>
<artifactId>hamcrest-matcher-generator</artifactId>
<version>${hamcrest-matcher-generator.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>io.github.marmer.testutils</groupId>
<artifactId>assertj-assertion-generator</artifactId>
<version>${assertj-assertion-generator.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
The user guide covers the annotation processor path, the configuration and the first test.