Close

AssertJ - String Assertions

[Last Updated: Mar 3, 2026]

AssertJ provides an extensive set of string-specific assertions accessible when you pass a String (or any CharSequence) to assertThat(). These cover length checks, substring presence, prefix/suffix matching, regular expression matching, and case-insensitive comparisons.

Use Cases

String assertions are valuable when testing output messages, formatted values, parsed text, or any domain logic that produces string results. They reduce boilerplate compared to manual assertTrue(str.contains(...)) calls and give better failure messages.

Key String Assertion Methods

Some commonly used string assertions include: isEqualTo, isEqualToIgnoringCase, contains, containsIgnoringCase, startsWith, endsWith, matches (regex), hasSize, isBlank, isNotBlank, doesNotContain.

Example

package com.logicbig.example;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

public class StringAssertionsExample {

    @Test
    void myTest() {
        String value = "Hello, AssertJ World!";

        // prefix / suffix / contains
        assertThat(value).startsWith("Hello")
                         .endsWith("World!")
                         .contains("AssertJ")
                         .doesNotContain("JUnit");
        System.out.println("Prefix/suffix checks passed");

        // case-insensitive
        assertThat(value).isEqualToIgnoringCase("hello, assertj world!");
        System.out.println("Case-insensitive check passed");

        // length
        assertThat(value).hasSizeGreaterThan(5)
                         .hasSizeLessThan(100);
        System.out.println("Length checks passed");

        // regex
        assertThat(value).matches(".*AssertJ.*");
        System.out.println("Regex check passed");

        // blank / empty
        assertThat("").isEmpty();
        assertThat("  ").isBlank();
        System.out.println("Blank/empty checks passed");
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.logicbig.example:assertj-string-assertions >-----------
[INFO] Building assertj-string-assertions 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-string-assertions ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-string-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-string-assertions\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-string-assertions ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-string-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-string-assertions\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-string-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-string-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.StringAssertionsExample
Prefix/suffix checks passed
Case-insensitive check passed
Length checks passed
Regex check passed
Blank/empty checks passed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.129 s -- in com.logicbig.example.StringAssertionsExample
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.379 s
[INFO] Finished at: 2026-03-03T11:06:29+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

The output confirms all string assertions pass, demonstrating how AssertJ's string-specific methods cover a wide range of text validation scenarios. The chained style keeps test code compact while each method clearly expresses the intent of the check.

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 - String Assertions Select All Download
  • assertj-string-assertions
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • StringAssertionsExample.java

    See Also

    Join