Close

AssertJ - Soft Assertions

[Last Updated: Mar 3, 2026]

By default, AssertJ stops test execution at the first failing assertion. This is usually desirable, but sometimes you want to see all failures at once — for example, when validating many fields on a response object. SoftAssertions collects all failures and reports them together after assertAll() is called.

Use Cases

Soft assertions are valuable for validating complex domain objects with many fields, UI form submissions, or API responses where you want the complete picture of what is wrong rather than fixing failures one by one.

Usage Styles

AssertJ provides three ways to use soft assertions:

1. Manual: create a SoftAssertions instance, make assertions on it, then call assertAll().

2. Auto-closeable: use try-with-resources — assertAll() is called automatically on close.

3. Lambda: SoftAssertions.assertSoftly(softly -> { ... }) — clean and self-contained.

Example

package com.logicbig.example;

import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

public class SoftAssertionsExample {

    record Product(String name,
                   double price,
                   boolean available) {}

    @Test
    void myTest() {
        Product product = new Product("Widget", 9.99, true);

        // try-with-resources style
        try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
            softly.assertThat(product.name()).isNotBlank();
            softly.assertThat(product.price()).isLessThan(100);
        }
        System.out.println("Try-with-resources style passed");
        // lambda style - all assertions run even if some fail
        SoftAssertions.assertSoftly(softly -> {
            softly.assertThat(product.name()).isEqualTo("Widget");
            softly.assertThat(product.price()).isGreaterThan(20);
            softly.assertThat(product.available()).isTrue();
            System.out.println("All soft asserts performed");
        });
        System.out.println("All soft assertions passed");
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] ------------< com.logicbig.example:assertj-soft-assertions >------------
[INFO] Building assertj-soft-assertions 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-soft-assertions ---
[INFO] Deleting D:\example-projects\assertj\assertj-soft-assertions\target
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-soft-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-soft-assertions\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-soft-assertions ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-soft-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-soft-assertions\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-soft-assertions ---
[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-soft-assertions ---
[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.SoftAssertionsExample
Try-with-resources style passed
All soft asserts performed
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.874 s <<< FAILURE! -- in com.logicbig.example.SoftAssertionsExample
[ERROR] com.logicbig.example.SoftAssertionsExample.myTest -- Time elapsed: 0.849 s <<< FAILURE!
org.assertj.core.error.AssertJMultipleFailuresError:

Multiple Failures (1 failure)
-- failure 1 --
Expecting actual:
9.99
to be greater than:
20.0
at SoftAssertionsExample.lambda$myTest$0(SoftAssertionsExample.java:26)
at com.logicbig.example.SoftAssertionsExample.myTest(SoftAssertionsExample.java:24)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] SoftAssertionsExample.myTest:24
Multiple Failures (1 failure)
-- failure 1 --
Expecting actual:
9.99
to be greater than:
20.0
at SoftAssertionsExample.lambda$myTest$0(SoftAssertionsExample.java:26)
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.062 s
[INFO] Finished at: 2026-03-03T14:07:37+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test (default-test) on project assertj-soft-assertions: There are test failures.
[ERROR]
[ERROR] Please refer to D:\example-projects\assertj\assertj-soft-assertions\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Conclusion

The example demonstrates the lambda style of soft assertions. All assertions execute regardless of individual pass or fail status — only at the end does AssertJ report combined results. This pattern is especially useful during development when you want to see the full scope of validation issues at once.

Example Project

Dependencies 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

AssertJ - Soft Assertions Select All Download
  • assertj-soft-assertions
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • SoftAssertionsExample.java

    See Also

    Join