Close

AssertJ - BDD-Style Assertions

[Last Updated: Mar 3, 2026]

Behavior-Driven Development (BDD) promotes structuring tests as Given-When-Then scenarios. AssertJ supports this style through the BDDAssertions class, which provides the same assertion methods as Assertions but with then() replacing assertThat(). The word "then" aligns with the third phase of a BDD scenario.

Why BDD Style?

When you write tests that read like specifications — given some input, when an action is performed, then the result should satisfy some condition — using then() makes the assertion phase explicit and the test more self-documenting. It also separates naturally from the arrange and act phases.

Importing BDDAssertions

Import statically from org.assertj.core.api.BDDAssertions:

import static org.assertj.core.api.BDDAssertions.then;

Or use BDDSoftAssertions to combine BDD style with soft assertions.

Example

package com.logicbig.example;

import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.BDDAssertions.then;

public class BddAssertionsExample {

    static int add(int a,
                   int b) {return a + b;}

    @Test
    void myTest() {
        // Given-When-Then structure
        // given
        int a = 5, b = 3;

        // when
        int result = add(a, b);

        // then
        then(result).isEqualTo(8).isPositive();
        System.out.println("BDD integer check passed");

        // BDD with collections
        List<String> names = List.of("Alice", "Bob");
        then(names).hasSize(2).contains("Alice");
        System.out.println("BDD collection check passed");

        // BDD with string
        String greeting = "Hello, World";
        then(greeting).startsWith("Hello").endsWith("World");
        System.out.println("BDD string check passed");
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] ------------< com.logicbig.example:assertj-bdd-assertions >-------------
[INFO] Building assertj-bdd-assertions 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-bdd-assertions ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-bdd-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-bdd-assertions\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-bdd-assertions ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-bdd-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-bdd-assertions\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-bdd-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-bdd-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.BddAssertionsExample
BDD integer check passed
BDD collection check passed
BDD string check passed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.164 s -- in com.logicbig.example.BddAssertionsExample
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.305 s
[INFO] Finished at: 2026-03-03T14:08:09+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

The output confirms that then() assertions behave identically to assertThat() while improving test readability. The BDD structure makes each test read as a natural English statement, aligning test code with the intent expressed in user stories or acceptance criteria.

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 - BDD-Style Assertions Select All Download
  • assertj-bdd-assertions
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • BddAssertionsExample.java

    See Also

    Join