An AssertJ Condition<T> is a named, reusable predicate that can be passed to is(), has(), satisfies(), and collection methods like are() and have(). Conditions are useful when the same logical check is applied in multiple tests, or when you want a human-readable name for a complex predicate.
Combining Conditions
AssertJ provides combinators: allOf(c1, c2) requires all conditions to hold, anyOf(c1, c2) requires at least one to hold, and not(condition) inverts the condition.
Creating a Condition
Condition<String> uppercase = new Condition<>(s -> s.equals(s.toUpperCase()), "uppercase");
assertThat("HELLO").is(uppercase);
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import org.assertj.core.api.Condition;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
public class ConditionExample {
@Test
void myTest() {
// define reusable conditions
Condition<String> longWord = new Condition<>(s -> s.length() > 5,
"long word");
Condition<String> startsWithA = new Condition<>(s -> s.startsWith("A"),
"starts with A");
assertThat("Algorithm").is(longWord);
System.out.println("longWord condition passed");
assertThat("Algorithm").is(startsWithA);
System.out.println("startsWithA condition passed");
// allOf combinator
assertThat("Algorithm").is(allOf(longWord, startsWithA));
System.out.println("allOf passed");
// anyOf combinator
assertThat("JavaProgramming").is(anyOf(longWord, startsWithA));
System.out.println("anyOf passed");
// collection with condition
List<String> words = List.of("Algorithm", "Array", "Abstract");
assertThat(words).are(startsWithA);
System.out.println("Collection condition passed");
}
}
Output$ mvn clean test -Dtest=* [INFO] Scanning for projects... [INFO] [INFO] ---------------< com.logicbig.example:assertj-condition >--------------- [INFO] Building assertj-condition 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ assertj-condition --- [INFO] Deleting D:\example-projects\assertj\assertj-condition\target [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ assertj-condition --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-condition\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-condition --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-condition --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-condition\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-condition --- [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ assertj-condition --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ConditionExample longWord condition passed startsWithA condition passed allOf passed anyOf passed Collection condition passed [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.172 s -- in com.logicbig.example.ConditionExample [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.234 s [INFO] Finished at: 2026-03-03T14:22:26+08:00 [INFO] ------------------------------------------------------------------------
Conclusion
The output confirms that defined conditions correctly evaluate their predicates, and that allOf and anyOf compose them as expected. Encapsulating predicates as named Condition objects improves test readability and centralizes reusable validation logic.
Example ProjectDependencies and Technologies Used: - assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
- junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
- JDK 17
- Maven 3.9.11
|
|