When a collection contains domain objects, you often need to assert on a specific field rather than the whole object. AssertJ's extracting() pulls out a named property or method result from each element into a new assertion context, letting you chain normal value assertions. Combined with filteredOn(), you can narrow the collection before extracting.
Why extracting?
Without extracting(), you would need to manually map a list to a list of properties and then assert on that. AssertJ does this in one step, keeping assertions inline and readable. It supports method references, property name strings, and lambdas.
Extracting with tuples
When you need to assert on multiple fields at once, pass multiple property names to extracting() and use tuple() to describe the expected combined values.
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
public class ExtractingFilteringExample {
static class Person {
String name;
int age;
Person(String name,
int age) {
this.name = name;
this.age = age;
}
public String getName() {return name;}
public int getAge() {return age;}
}
@Test
void myTest() {
List<Person> people = List.of(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 30)
);
// extracting a single property
assertThat(people).extracting(Person::getName)
.contains("Alice", "Bob", "Charlie");
System.out.println("Name extraction passed");
// extracting multiple fields as tuples
assertThat(people).extracting("name", "age")
.contains(tuple("Alice", 30), tuple("Bob", 25));
System.out.println("Tuple extraction passed");
// filter then extract
assertThat(people).filteredOn(p -> p.age == 30)
.extracting(Person::getName)
.containsOnly("Alice", "Charlie");
System.out.println("FilteredOn + extracting passed");
}
}
Output$ mvn clean test -Dtest=* [INFO] Scanning for projects... [INFO] [INFO] ---------< com.logicbig.example:assertj-extracting-filtering >---------- [INFO] Building assertj-extracting-filtering 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ assertj-extracting-filtering --- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ assertj-extracting-filtering --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-extracting-filtering\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-extracting-filtering --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-extracting-filtering --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-extracting-filtering\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-extracting-filtering --- [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-extracting-filtering --- [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.ExtractingFilteringExample Name extraction passed Tuple extraction passed FilteredOn + extracting passed [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.104 s -- in com.logicbig.example.ExtractingFilteringExample [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.333 s [INFO] Finished at: 2026-03-03T13:26:57+08:00 [INFO] ------------------------------------------------------------------------
Conclusion
The output confirms that extracting correctly pulled the name field from each Person object, and filteredOn narrowed down the list before extracting. This pattern removes the need for manual mapping in tests and keeps assertions focused.
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
|
|