CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-truth--truth

Fluent assertion framework for Java that provides clear, readable test assertions and informative failure messages

Pending
Overview
Eval results
Files

string-assertions.mddocs/

String Assertions

Comprehensive string-specific assertion methods for length, content, pattern matching, and case-insensitive comparisons.

Capabilities

String Subject Creation

/**
 * Creates a StringSubject for asserting about String values.
 * @param actual the String under test
 */
public static StringSubject assertThat(String actual);

Length Assertions

Methods for asserting about string length and emptiness.

/**
 * Fails if the string does not have the given length.
 * @param expectedLength the expected length
 */
public void hasLength(int expectedLength);

/**
 * Fails if the string is not empty.
 */
public void isEmpty();

/**
 * Fails if the string is empty.
 */
public void isNotEmpty();

Usage Examples:

assertThat("hello").hasLength(5);
assertThat("").isEmpty();
assertThat("non-empty").isNotEmpty();

Content Assertions

Methods for asserting about string content and substrings.

/**
 * Fails if the string does not contain the given sequence.
 * @param expectedSubstring the substring that should be present
 */
public void contains(CharSequence expectedSubstring);

/**
 * Fails if the string contains the given sequence.
 * @param unexpectedSubstring the substring that should not be present  
 */
public void doesNotContain(CharSequence unexpectedSubstring);

/**
 * Fails if the string does not start with the given string.
 * @param prefix the expected prefix
 */
public void startsWith(String prefix);

/**
 * Fails if the string does not end with the given string.
 * @param suffix the expected suffix
 */
public void endsWith(String suffix);

Usage Examples:

assertThat("hello world").contains("world");
assertThat("hello world").doesNotContain("goodbye");
assertThat("prefix_suffix").startsWith("prefix");
assertThat("prefix_suffix").endsWith("suffix");

Pattern Matching Assertions

Methods for regex pattern matching and validation.

/**
 * Fails if the string does not match the given regular expression.
 * @param regex the regular expression that should match
 */
public void matches(String regex);

/**
 * Fails if the string does not match the given pattern.
 * @param pattern the Pattern that should match
 */
public void matches(Pattern pattern);

/**
 * Fails if the string matches the given regular expression.
 * @param regex the regular expression that should not match
 */
public void doesNotMatch(String regex);

/**
 * Fails if the string matches the given pattern.
 * @param pattern the Pattern that should not match
 */
public void doesNotMatch(Pattern pattern);

/**
 * Fails if the string does not contain a match for the given regular expression.
 * @param regex the regular expression that should have a match within the string
 */
public void containsMatch(String regex);

/**
 * Fails if the string does not contain a match for the given pattern.
 * @param pattern the Pattern that should have a match within the string
 */
public void containsMatch(Pattern pattern);

/**
 * Fails if the string contains a match for the given regular expression.
 * @param regex the regular expression that should not have a match within the string
 */
public void doesNotContainMatch(String regex);

/**
 * Fails if the string contains a match for the given pattern.
 * @param pattern the Pattern that should not have a match within the string
 */
public void doesNotContainMatch(Pattern pattern);

Usage Examples:

import java.util.regex.Pattern;

// Full string matching
assertThat("abc123").matches("\\w+\\d+");
assertThat("abc123").matches(Pattern.compile("\\w+\\d+"));
assertThat("abc").doesNotMatch("\\d+");

// Substring matching
assertThat("hello123world").containsMatch("\\d+");
assertThat("hello world").doesNotContainMatch("\\d+");

// Email validation example
assertThat("user@example.com").matches("\\w+@\\w+\\.\\w+");

Case-Insensitive Assertions

Methods for case-insensitive string comparisons.

/**
 * Returns a CaseInsensitiveStringComparison for case-insensitive assertions.
 */
public CaseInsensitiveStringComparison ignoringCase();

/**
 * Nested class providing case-insensitive string comparison methods.
 */
public static class CaseInsensitiveStringComparison {
    /**
     * Fails if the string is not equal to the given string, ignoring case differences.
     * @param expected the expected string value (case-insensitive)
     */
    public void isEqualTo(String expected);
    
    /**
     * Fails if the string is equal to the given string, ignoring case differences.
     * @param unexpected the string value that should not match (case-insensitive)
     */
    public void isNotEqualTo(String unexpected);
    
    /**
     * Fails if the string does not contain the given sequence, ignoring case differences.
     * @param expectedSubstring the substring that should be present (case-insensitive)
     */
    public void contains(CharSequence expectedSubstring);
    
    /**
     * Fails if the string contains the given sequence, ignoring case differences.
     * @param unexpectedSubstring the substring that should not be present (case-insensitive)
     */
    public void doesNotContain(CharSequence unexpectedSubstring);
}

Usage Examples:

assertThat("Hello World").ignoringCase().isEqualTo("hello world");
assertThat("Hello World").ignoringCase().isNotEqualTo("goodbye world");
assertThat("Hello World").ignoringCase().contains("HELLO");
assertThat("Hello World").ignoringCase().doesNotContain("GOODBYE");

Advanced String Validation Examples

Common patterns for string validation in testing scenarios:

// URL validation
assertThat(url).startsWith("https://");
assertThat(url).containsMatch("^https?://[\\w.-]+\\.[a-zA-Z]{2,}");

// File path validation  
assertThat(filePath).endsWith(".java");
assertThat(filePath).contains("/src/main/");

// Multi-line text validation
String multiLineText = "Line 1\nLine 2\nLine 3";
assertThat(multiLineText).containsMatch("Line \\d");
assertThat(multiLineText).contains("Line 2");

// JSON-like string validation
String jsonLike = "{\"name\":\"John\",\"age\":30}";
assertThat(jsonLike).startsWith("{");
assertThat(jsonLike).endsWith("}");
assertThat(jsonLike).containsMatch("\"\\w+\":");

// Case-insensitive configuration values
String configValue = "TrUe";
assertThat(configValue).ignoringCase().isEqualTo("true");

Types

/**
 * Subject class for making assertions about String values.
 */
public class StringSubject extends Subject {
    /**
     * Constructor for StringSubject.
     * @param metadata failure metadata for context
     * @param actual the String under test
     */
    protected StringSubject(FailureMetadata metadata, String actual);
}

/**
 * Provides case-insensitive string comparison methods.
 */
public static class CaseInsensitiveStringComparison {
    // Methods documented above
}

Install with Tessl CLI

npx tessl i tessl/maven-com-google-truth--truth

docs

array-assertions.md

collection-assertions.md

core-assertions.md

custom-assertions.md

exception-assertions.md

index.md

java8-assertions.md

map-assertions.md

numeric-assertions.md

string-assertions.md

testing-utilities.md

tile.json