A Gradle plugin that generates AssertJ assertion classes for your classes and packages.
The generateAssertions task runs after classes and before the test compilation tasks,
and generated sources are added to the test source set automatically.
- Gradle 8.5 or newer, tested 8.5–9.x (users on Gradle 7.x–8.4 should stay on plugin version 1.1.5)
jakarta.annotation:jakarta.annotation-apion the test compile classpath, or a stack that includes it (e.g. Spring Boot 3+). See Generated annotations.
plugins {
id 'com.github.fhermansson.assertj-generator' version '2.0.1'
}
repositories { mavenCentral() }
dependencies {
testImplementation 'org.assertj:assertj-core:3.27.7'
testCompileOnly 'jakarta.annotation:jakarta.annotation-api:2.1.1'
}
assertjGenerator {
classOrPackageNames = ['com.example.model']
}Use any assertj-core version you like in your tests — the version the plugin uses internally (for generation only) is an implementation detail and does not constrain yours. Generated assertions compile against your test classpath.
Run ./gradlew test — assertions are generated, compiled, and available via the generated
entry points, e.g. import static com.example.model.Assertions.assertThat;.
Generated classes are annotated @jakarta.annotation.Generated. No JDK has shipped the
javax.annotation class since Java 11, so the annotation always comes from a dependency —
modern stacks (Jakarta EE 9+, Spring Boot 3+) already have it.
The annotation has SOURCE retention: it is needed only to compile the generated code
(testCompileOnly is enough) and leaves no trace in bytecode or at runtime.
The assertjGenerator extension configures the generateAssertions task and provides
defaults for additional tasks of type com.github.fhermansson.gradle.assertj.plugin.GenerateAssertions.
All properties support = assignment in both Groovy and Kotlin DSL.
| Property | Type | Default | Description |
|---|---|---|---|
| classOrPackageNames | ListProperty<String> | [] | Class or package names to generate assertions for |
| sourceSet | Property<SourceSet> | sourceSets.main | The sourceSet containing classes to generate assertions for. The task depends on its classes task. |
| testSourceSet | Property<SourceSet> | sourceSets.test | The target sourceSet. outputDir is added to its srcDirs, and its compileJava, compileKotlin and compileGroovy tasks depend on this task. |
| outputDir | DirectoryProperty | [buildDirectory]/generated/sources/assertj/[testSourceSet.name] | Where to put the generated classes |
| entryPointPackage | Property<String> | unset | Destination package for entry point classes. The generator derives it from the generated classes if unset |
| entryPointTypes | SetProperty<AssertionsEntryPointType> | ['STANDARD'] | Types of entry point classes to generate. Values: 'STANDARD', 'SOFT', 'BDD', 'JUNIT_SOFT', 'BDD_SOFT', 'JUNIT_BDD_SOFT', 'AUTO_CLOSEABLE_SOFT', 'AUTO_CLOSEABLE_BDD_SOFT' |
| entryPointInherits | Property<Boolean> | true | Entry point classes inherit from core AssertJ classes |
| cleanOutputDir | Property<Boolean> | true | Remove all files in outputDir before generating assertions |
| excludes | ListProperty<String> | [] | Regexes matched against fully qualified class names; matching classes are skipped |
| generateForNonPublicFields | Property<Boolean> | false | Generate assertions for non-public fields without public accessors as well |
For each additional source set, register an extra task. Properties you set on the task
win; unset task properties fall back to the assertjGenerator extension, and to plugin
defaults if the extension does not set them either:
import com.github.fhermansson.gradle.assertj.plugin.GenerateAssertions
assertjGenerator {
classOrPackageNames = ['com.example.model']
entryPointTypes = ['STANDARD', 'SOFT']
}
tasks.register('generateOtherAssertions', GenerateAssertions) {
classOrPackageNames = ['com.other.model']
sourceSet = sourceSets.other
testSourceSet = sourceSets.otherTest
}This also works with source sets from other plugins, e.g. java-test-fixtures:
tasks.register('generateTestFixturesAssertions', GenerateAssertions) {
sourceSet = sourceSets.testFixtures
}Three changes in 2.0.0:
- The default
outputDirmoved fromsrc/[testSourceSet.name]/generated-javato[buildDirectory]/generated/sources/assertj/[testSourceSet.name]. Generated sources now live under the build directory: nothing to commit or .gitignore, andgradle cleanremoves them. If you relied on the old location, set it explicitly:
assertjGenerator {
outputDir = file('src/test/generated-java')
}-
Generated classes are now annotated
@jakarta.annotation.Generated(theuseJakartaAnnotationsoption is gone — jakarta is always used). See Generated annotations. -
The extension now uses Gradle's lazy
Propertytypes.=assignment works in both Groovy and Kotlin DSL; list-typed properties changed from arrays to lists, andoutputDiris now aDirectoryPropertythat takes files or directory providers. -
If you have kotlinter lint/format workarounds for the generated directory (
exclude { ... }andmustRunAfter/dependsOnonLintTask/FormatTask), remove them — they are no longer needed since 2.0.1: the generated sources directory is wired as a task output provider, so consuming tasks carry the dependency implicitly.