Class AbstractCharSequenceAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractCharSequenceAssert<SELF,ACTUAL>
-
- Type Parameters:
SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.ACTUAL
- the type of the "actual" value.
- All Implemented Interfaces:
Assert<SELF,ACTUAL>
,Descriptable<SELF>
,EnumerableAssert<SELF,Character>
,ExtensionPoints<SELF,ACTUAL>
- Direct Known Subclasses:
CharSequenceAssert
,StringAssert
public abstract class AbstractCharSequenceAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence> extends AbstractAssert<SELF,ACTUAL> implements EnumerableAssert<SELF,Character>
Base class for all implementations of assertions forCharSequence
s.- Author:
- Yvonne Wang, David DIDIER, Alex Ruiz, Joel Costigliola, Mikhail Mazursky, Nicolas Francois
-
-
Field Summary
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself
-
-
Constructor Summary
Constructors Constructor Description AbstractCharSequenceAssert(ACTUAL actual, Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SELF
contains(CharSequence... values)
Verifies that the actualCharSequence
contains all the given values.SELF
contains(Iterable<? extends CharSequence> values)
Verifies that the actualCharSequence
contains all theCharSequence
s of the given Iterable.SELF
containsIgnoringCase(CharSequence sequence)
Verifies that the actualCharSequence
contains the given sequence, ignoring case considerations.SELF
containsOnlyDigits()
Verifies that the actualCharSequence
contains only digits.SELF
containsOnlyOnce(CharSequence sequence)
Verifies that the actualCharSequence
contains the given sequence only once.SELF
containsPattern(CharSequence regex)
Verifies that the actualCharSequence
contains the given regular expression.SELF
containsPattern(Pattern pattern)
Verifies that the actualCharSequence
contains the given regular expression pattern.SELF
containsSequence(CharSequence... values)
Verifies that the actualCharSequence
contains all the given values in the given order.SELF
containsSequence(Iterable<? extends CharSequence> values)
Verifies that the actualCharSequence
contains all the values of the given Iterable in the Iterable iteration order.SELF
doesNotContain(CharSequence sequence)
Verifies that the actualCharSequence
does not contain the given sequence.SELF
doesNotContainPattern(CharSequence pattern)
Verifies that the actualCharSequence
does not contain the given regular expression.SELF
doesNotContainPattern(Pattern pattern)
Verifies that the actualCharSequence
does not contain the given regular expression.SELF
doesNotEndWith(CharSequence suffix)
Verifies that the actualCharSequence
does not end with the given suffix.SELF
doesNotMatch(CharSequence regex)
Verifies that the actualCharSequence
does not match the given regular expression.SELF
doesNotMatch(Pattern pattern)
Verifies that the actualCharSequence
does not match the given regular expression pattern.SELF
doesNotStartWith(CharSequence prefix)
Verifies that the actualCharSequence
does not start with the given prefix.SELF
endsWith(CharSequence suffix)
Verifies that the actualCharSequence
ends with the given suffix.SELF
hasLineCount(int expectedLineCount)
Verifies that the actualCharSequence
has the expected line count.SELF
hasSameSizeAs(CharSequence other)
Verifies that the actualCharSequence
has a length that's the same as the length of the givenCharSequence
.SELF
hasSameSizeAs(Iterable<?> other)
Verifies that the actualCharSequence
has a length that's the same as the number of elements in the given Iterable.SELF
hasSameSizeAs(Object other)
Verifies that the actualCharSequence
has a length that's the same as the number of elements in the given array.SELF
hasSize(int expected)
Verifies that the actualCharSequence
has the expected length using thelength()
method.SELF
inHexadecimal()
Use hexadecimal object representation instead of standard representation in error messages.SELF
inUnicode()
Use unicode character representation instead of standard representation in error messages.SELF
isBlank()
Verifies that the actualCharSequence
is blank, i.e.void
isEmpty()
Verifies that the actualCharSequence
is empty, i.e., it has a length of 0 and is notnull
.SELF
isEqualToIgnoringCase(CharSequence expected)
Verifies that the actualCharSequence
is equal to the given one, ignoring case considerations.SELF
isEqualToIgnoringWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is equal to the given one, ignoring whitespace differencesSELF
isEqualToNormalizingNewlines(CharSequence expected)
Verifies that the actualCharSequence
is equals to anotherCharSequence
after normalizing new line characters (i.e.SELF
isEqualToNormalizingWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is equal to the given one, after the whitespace of both strings has been normalized.
To be exact, the following rules are applied: all leading and trailing whitespace of both actual and expected strings are ignored any remaining whitespace, appearing within either string, is collapsed to a single space before comparisonSELF
isJavaBlank()
Verifies that the actualCharSequence
is blank, i.e.SELF
isNotBlank()
Verifies that the actualCharSequence
is not blank, i.e.SELF
isNotEmpty()
Verifies that the actualCharSequence
is not empty, i.e., is notnull
and has a length of 1 or more.SELF
isNotEqualToIgnoringCase(CharSequence expected)
Verifies that the actualCharSequence
is not equal to the given one, ignoring case considerations.SELF
isNotEqualToIgnoringWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is not equal to the given one, ignoring whitespace differences.SELF
isNotEqualToNormalizingWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is not equal to the given one, after the whitespace of both strings has been normalized.
To be exact, the following rules are applied: all leading and trailing whitespace of both actual and expected strings are ignored any remaining whitespace, appearing within either string, is collapsed to a single space before comparisonSELF
isNotJavaBlank()
Verifies that the actualCharSequence
is not blank, i.e.void
isNullOrEmpty()
Verifies that the actualCharSequence
is empty, i.e., it has a length of 0, or isnull
.SELF
isSubstringOf(CharSequence sequence)
Verifies that the actualCharSequence
is a substring of the given one (opposite assertion ofcontains(CharSequence cs)
.SELF
isXmlEqualTo(CharSequence expectedXml)
Verifies that the actualCharSequence
is equal to the given XMLCharSequence
after both have been formatted the same way.SELF
isXmlEqualToContentOf(File xmlFile)
Verifies that the actualCharSequence
is equal to the content of the given file.SELF
matches(CharSequence regex)
Verifies that the actualCharSequence
matches the given regular expression.SELF
matches(Pattern pattern)
Verifies that the actualCharSequence
matches the given regular expression pattern.SELF
startsWith(CharSequence prefix)
Verifies that the actualCharSequence
starts with the given prefix.SELF
usingComparator(Comparator<? super ACTUAL> customComparator)
Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingDefaultComparator()
Revert to standard comparison for incoming assertion checks.SELF
usingDefaultElementComparator()
Deprecated.Custom element Comparator is not supported for CharSequence comparison.SELF
usingElementComparator(Comparator<? super Character> customComparator)
Deprecated.Custom element Comparator is not supported for CharSequence comparison.-
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasToString, inBinary, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, overridingErrorMessage, satisfies, setCustomRepresentation, throwAssertionError, withFailMessage, withRepresentation, withThreadDumpOnError
-
-
-
-
Method Detail
-
isNullOrEmpty
public void isNullOrEmpty()
Verifies that the actualCharSequence
is empty, i.e., it has a length of 0, or isnull
.If you do not want to accept a
null
value, useisEmpty()
instead.Both of these assertions will succeed:
Whereas these assertions will fail:String emptyString = "" assertThat(emptyString).isNullOrEmpty(); String nullString = null; assertThat(nullString).isNullOrEmpty();
assertThat("a").isNullOrEmpty(); assertThat(" ").isNullOrEmpty();
- Specified by:
isNullOrEmpty
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Throws:
AssertionError
- if the actualCharSequence
has a non-zero length.
-
isEmpty
public void isEmpty()
Verifies that the actualCharSequence
is empty, i.e., it has a length of 0 and is notnull
.If you want to accept a
null
value as well as a 0 length, useisNullOrEmpty()
instead.This assertion will succeed:
Whereas these assertions will fail:String emptyString = "" assertThat(emptyString).isEmpty();
String nullString = null; assertThat(nullString).isEmpty(); assertThat("a").isEmpty(); assertThat(" ").isEmpty();
- Specified by:
isEmpty
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Throws:
AssertionError
- if the actualCharSequence
has a non-zero length or is null.
-
isNotEmpty
public SELF isNotEmpty()
Verifies that the actualCharSequence
is not empty, i.e., is notnull
and has a length of 1 or more.This assertion will succeed:
Whereas these assertions will fail:String bookName = "A Game of Thrones" assertThat(bookName).isNotEmpty();
String emptyString = "" assertThat(emptyString).isNotEmpty(); String nullString = null; assertThat(nullString).isNotEmpty();
- Specified by:
isNotEmpty
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is empty (has a length of 0).
-
isBlank
public SELF isBlank()
Verifies that the actualCharSequence
is blank, i.e. is notnull
or empty and contains only whitespace characters.The whitespace definition used in this assertion follows the latest Unicode standard (which is not the same as Java whitespace definition) and is based on Guava CharMatcher#whitespace. If you want to stick with Java whitespace definition, use
isJavaBlank()
.These assertions will succeed:
Whereas these assertions will fail:assertThat(" ").isBlank(); assertThat(" ").isBlank();
assertThat("a").isBlank(); assertThat(" b").isBlank(); assertThat("").isBlank(); String nullString = null; assertThat(nullString).isNotBlank();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not blank.- Since:
- 2.6.0 / 3.6.0
-
isNotBlank
public SELF isNotBlank()
Verifies that the actualCharSequence
is not blank, i.e. either isnull
, empty or contains at least one whitespace characters.It uses the same whitespace definition as in
isBlank()
assertion.These assertion will succeed:
Whereas these assertions will fail:assertThat("a").isNotBlank(); assertThat(" b").isNotBlank(); assertThat(" c ").isNotBlank(); assertThat("").isNotBlank(); String nullString = null; assertThat(nullString).isNotBlank();
assertThat(" ").isNotBlank(); assertThat(" ").isNotBlank();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is blank.- Since:
- 2.6.0 / 3.6.0
-
isJavaBlank
public SELF isJavaBlank()
Verifies that the actualCharSequence
is blank, i.e. it contains only whitespace characters (according toCharacter.isWhitespace(char)
).If you want to use the latest Unicode standard whitespace definition (as in Guava), use
isBlank()
, see Guava explanation for more details.These assertions will succeed:
Whereas these assertions will fail:assertThat(" ").isBlank(); assertThat(" ").isBlank();
assertThat("a").isBlank(); assertThat(" b").isBlank(); assertThat("").isBlank(); String nullString = null; assertThat(nullString).isBlank();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not blank.- Since:
- 2.6.0 / 3.6.0
-
isNotJavaBlank
public SELF isNotJavaBlank()
Verifies that the actualCharSequence
is not blank, i.e. either isnull
, empty or contains at least one whitespace characters (according toCharacter.isWhitespace(char)
).It uses the same whitespace definition as in
isJavaBlank()
assertion.These assertion will succeed:
Whereas these assertions will fail:assertThat("a").isNotBlank(); assertThat(" b").isNotBlank(); assertThat(" c ").isNotBlank(); assertThat("").isNotBlank(); String nullString = null; assertThat(nullString).isNotBlank();
assertThat(" ").isNotBlank(); assertThat(" ").isNotBlank();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is blank.- Since:
- 2.6.0 / 3.6.0
-
hasSize
public SELF hasSize(int expected)
Verifies that the actualCharSequence
has the expected length using thelength()
method.This assertion will succeed:
Whereas this assertion will fail:String bookName = "A Game of Thrones" assertThat(bookName).hasSize(17);
String bookName = "A Clash of Kings" assertThat(bookName).hasSize(4);
- Specified by:
hasSize
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Parameters:
expected
- the expected length of the actualCharSequence
.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual length is not equal to the expected length.
-
hasLineCount
public SELF hasLineCount(int expectedLineCount)
Verifies that the actualCharSequence
has the expected line count.A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed (see
LineNumberReader
).This assertion will succeed:
Whereas this assertion will fail:String multiLine = "First line\n" + "Last line"; assertThat(multiLine).hasLineCount(2);
String bookName = "A Clash of Kings"; assertThat(bookName).hasLineCount(3);
- Parameters:
expectedLineCount
- the expected line count of the actualCharSequence
.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actual line count is not equal to the expected one.
-
hasSameSizeAs
public SELF hasSameSizeAs(CharSequence other)
Verifies that the actualCharSequence
has a length that's the same as the length of the givenCharSequence
.Examples :
// assertion will pass assertThat("C-3PO").hasSameSizeAs("R2-D2"); // assertion will fail as actual and expected sizes differ assertThat("C-3PO").hasSameSizeAs("B1 battle droid");
- Parameters:
other
- the givenCharSequence
to be used for size comparison.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
has a length that's different from the length of the givenCharSequence
.NullPointerException
- if the givenCharSequence
isnull
.
-
hasSameSizeAs
public SELF hasSameSizeAs(Object other)
Verifies that the actualCharSequence
has a length that's the same as the number of elements in the given array.Example:
// assertion will pass assertThat("12").hasSameSizeAs(new char[] { 'a', 'b' }); // assertion will fail assertThat("12").hasSameSizeAs(new char[] { 'a', 'b', 'c' });
- Specified by:
hasSameSizeAs
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Parameters:
other
- the given array to be used for size comparison.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
has a length that's different from the number of elements in the array.NullPointerException
- if the given array isnull
.
-
hasSameSizeAs
public SELF hasSameSizeAs(Iterable<?> other)
Verifies that the actualCharSequence
has a length that's the same as the number of elements in the given Iterable.Example:
// assertion will pass assertThat("abc").hasSameSizeAs(Arrays.asList(1, 2, 3)); // assertion will fail assertThat("ab").hasSameSizeAs(Arrays.asList(1, 2, 3));
- Specified by:
hasSameSizeAs
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Parameters:
other
- the givenIterable
to be used for size comparison.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
has a length that's different from the number of elements in theIterable
.NullPointerException
- if the givenIterable
isnull
.
-
isEqualToIgnoringCase
public SELF isEqualToIgnoringCase(CharSequence expected)
Verifies that the actualCharSequence
is equal to the given one, ignoring case considerations.Example :
// assertion will pass assertThat("Gandalf the grey").isEqualToIgnoringCase("GaNdAlF tHe GREY"); assertThat("Gandalf the grey").isEqualToIgnoringCase("Gandalf the grey"); // assertion will fail assertThat("Gandalf the grey").isEqualToIgnoringCase("Gandalf the white");
- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not equal to the given one.
-
isNotEqualToIgnoringCase
public SELF isNotEqualToIgnoringCase(CharSequence expected)
Verifies that the actualCharSequence
is not equal to the given one, ignoring case considerations.Example :
// assertions will pass assertThat("Gandalf").isNotEqualToIgnoringCase("Hobbit"); assertThat("Gandalf").isNotEqualToIgnoringCase("HOBit"); assertThat((String)null).isNotEqualToIgnoringCase("Gandalf"); assertThat("Gandalf").isNotEqualToIgnoringCase(null); // assertions will fail assertThat("Gandalf").isNotEqualToIgnoringCase("Gandalf"); assertThat("Gandalf").isNotEqualToIgnoringCase("GaNDalf"); assertThat((String)null).isNotEqualToIgnoringCase(null);
- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not equal to the given one.
-
containsOnlyDigits
public SELF containsOnlyDigits()
Verifies that the actualCharSequence
contains only digits. It fails if it contains non-digit characters or is empty.This assertion succeeds:
Whereas this assertion fails:assertThat("10").containsOnlyDigits();
assertThat("10$").containsOnlyDigits(); assertThat("").containsOnlyDigits();
- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
contains non-digit characters or isnull
.
-
containsOnlyOnce
public SELF containsOnlyOnce(CharSequence sequence)
Verifies that the actualCharSequence
contains the given sequence only once.Example :
// assertion will pass assertThat("Frodo").containsOnlyOnce("do"); // assertions will fail assertThat("Frodo").containsOnlyOnce("o"); assertThat("Frodo").containsOnlyOnce("y");
- Parameters:
sequence
- the sequence to search for.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
either does not contain the given one at all, or contains it more than once.
-
contains
public SELF contains(CharSequence... values)
Verifies that the actualCharSequence
contains all the given values.You can use one or several
CharSequence
s as in this example:assertThat("Gandalf the grey").contains("alf"); assertThat("Gandalf the grey").contains("alf", "grey");
- Parameters:
values
- the Strings to look for.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given list of values isnull
.IllegalArgumentException
- if the list of given values is empty.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not contain all the given strings.
-
contains
public SELF contains(Iterable<? extends CharSequence> values)
Verifies that the actualCharSequence
contains all theCharSequence
s of the given Iterable.Examples:
assertThat("Gandalf the grey").contains(Arrays.asList("alf")); assertThat("Gandalf the grey").contains(Arrays.asList("alf", "grey"));
- Parameters:
values
- the Strings to look for.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given list of values isnull
.IllegalArgumentException
- if the list of given values is empty.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not contain all the given strings.
-
containsSequence
public SELF containsSequence(CharSequence... values)
Verifies that the actualCharSequence
contains all the given values in the given order.Note that unlike
IterableAssert.containsSequence(Object...)
, the assertion will succeed when there are values between the expected sequence values.Example:
String book = "{ 'title':'A Game of Thrones', 'author':'George Martin'}"; // this assertions succeeds assertThat(book).containsSequence("'title'", ":", "'A Game of Thrones'"); // this one too even if there are values between the expected sequence (e.g "'title':'") assertThat(book).containsSequence("{", "A Game of Thrones", "George Martin", "}"); // this one fails as "author" must come after "A Game of Thrones" assertThat(book).containsSequence("{", "author", "A Game of Thrones", "}");
- Parameters:
values
- the Strings to look for, in order.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given values isnull
.IllegalArgumentException
- if the given values is empty.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not contain all the given strings in the given order.
-
containsSequence
public SELF containsSequence(Iterable<? extends CharSequence> values)
Verifies that the actualCharSequence
contains all the values of the given Iterable in the Iterable iteration order.Note that unlike
IterableAssert.containsSequence(Object...)
, the assertion will succeed when there are values between the expected sequence values.Example:
String book = "{ 'title':'A Game of Thrones', 'author':'George Martin'}"; // this assertion succeeds assertThat(book).containsSequence(asList("{", "title", "A Game of Thrones", "}")); // this one too even if there are values between the expected sequence (e.g "'title':'") assertThat(book).containsSequence(asList("{", "A Game of Thrones", "George Martin", "}")); // but this one fails as "author" must come after "A Game of Thrones" assertThat(book).containsSequence(asList("{", "author", "A Game of Thrones", "}"));
- Parameters:
values
- the Strings to look for, in order.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given values isnull
.IllegalArgumentException
- if the given values is empty.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not contain all the given strings in the given order.
-
containsIgnoringCase
public SELF containsIgnoringCase(CharSequence sequence)
Verifies that the actualCharSequence
contains the given sequence, ignoring case considerations.Example :
// assertion will pass assertThat("Gandalf the grey").containsIgnoringCase("gandalf"); // assertion will fail assertThat("Gandalf the grey").containsIgnoringCase("white");
- Parameters:
sequence
- the sequence to search for.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given sequence isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not contain the given one.
-
doesNotContain
public SELF doesNotContain(CharSequence sequence)
Verifies that the actualCharSequence
does not contain the given sequence.Example :
// assertion will pass assertThat("Frodo").doesNotContain("fro"); assertThat("Frodo").doesNotContain("gandalf"); // assertion will fail assertThat("Frodo").doesNotContain("Fro");
- Parameters:
sequence
- the sequence to search for.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given sequence isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
contains the given one.
-
doesNotContainPattern
public SELF doesNotContainPattern(CharSequence pattern)
Verifies that the actualCharSequence
does not contain the given regular expression.Example :
// assertion will pass assertThat("Frodo").doesNotContainPattern("Fr.ud"); // assertion will fail assertThat("Freud").doesNotContainPattern("Fr.ud");
- Parameters:
pattern
- the regular expression to find in the actualCharSequence
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.PatternSyntaxException
- if the regular expression's syntax is invalid.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the given regular expression can be found in the actualCharSequence
.- Since:
- 2.7.0 / 3.7.0
-
doesNotContainPattern
public SELF doesNotContainPattern(Pattern pattern)
Verifies that the actualCharSequence
does not contain the given regular expression.Example :
// assertion will pass assertThat("Frodo").doesNotContainPattern(Pattern.compile("Fr.ud")); // assertion will fail assertThat("Freud").doesNotContainPattern(Pattern.compile("Fr.ud"));
- Parameters:
pattern
- the regular expression to find in the actualCharSequence
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the given regular expression can be found in the actualCharSequence
.- Since:
- 2.7.0 / 3.7.0
-
startsWith
public SELF startsWith(CharSequence prefix)
Verifies that the actualCharSequence
starts with the given prefix.Example :
// assertions will pass assertThat("Frodo").startsWith("Fro"); assertThat("Gandalf the grey").startsWith("Gandalf"); // assertions will fail assertThat("Frodo").startsWith("fro"); assertThat("Gandalf the grey").startsWith("grey");
- Parameters:
prefix
- the given prefix.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given prefix isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not start with the given prefix.
-
doesNotStartWith
public SELF doesNotStartWith(CharSequence prefix)
Verifies that the actualCharSequence
does not start with the given prefix.Example:
// assertions will pass assertThat("Frodo").doesNotStartWith("fro"); assertThat("Gandalf the grey").doesNotStartWith("grey"); // assertions will fail assertThat("Gandalf the grey").doesNotStartWith("Gandalf"); assertThat("Frodo").doesNotStartWith("");
- Parameters:
prefix
- the given prefix.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given prefix isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
starts with the given prefix.
-
endsWith
public SELF endsWith(CharSequence suffix)
Verifies that the actualCharSequence
ends with the given suffix.Example :
// assertion will pass assertThat("Frodo").endsWith("do"); // assertion will fail assertThat("Frodo").endsWith("Fro");
- Parameters:
suffix
- the given suffix.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given suffix isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not end with the given suffix.
-
doesNotEndWith
public SELF doesNotEndWith(CharSequence suffix)
Verifies that the actualCharSequence
does not end with the given suffix.Example:
// assertion will pass assertThat("Frodo").doesNotEndWith("Fro"); // assertions will fail assertThat("Frodo").doesNotEndWith("do"); assertThat("Frodo").doesNotEndWith("");
- Parameters:
suffix
- the given suffix.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given suffix isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
ends with the given suffix.
-
matches
public SELF matches(CharSequence regex)
Verifies that the actualCharSequence
matches the given regular expression.Example :
// assertion will pass assertThat("Frodo").matches("..o.o"); // assertion will fail assertThat("Frodo").matches(".*d");
- Parameters:
regex
- the regular expression to which the actualCharSequence
is to be matched.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.PatternSyntaxException
- if the regular expression's syntax is invalid.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not match the given regular expression.
-
doesNotMatch
public SELF doesNotMatch(CharSequence regex)
Verifies that the actualCharSequence
does not match the given regular expression.Example :
// assertion will pass assertThat("Frodo").doesNotMatch(".*d"); // assertion will fail assertThat("Frodo").doesNotMatch("..o.o");
- Parameters:
regex
- the regular expression to which the actualCharSequence
is to be matched.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.PatternSyntaxException
- if the regular expression's syntax is invalid.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
matches the given regular expression.
-
matches
public SELF matches(Pattern pattern)
Verifies that the actualCharSequence
matches the given regular expression pattern.Example :
// assertion will pass assertThat("Frodo").matches(Pattern.compile("..o.o")); // assertion will fail assertThat("Frodo").matches(Pattern.compile(".*d"));
- Parameters:
pattern
- the regular expression to which the actualCharSequence
is to be matched.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the actualCharSequence
does not match the given regular expression.
-
doesNotMatch
public SELF doesNotMatch(Pattern pattern)
Verifies that the actualCharSequence
does not match the given regular expression pattern.Example :
// assertion will pass assertThat("Frodo").doesNotMatch(Pattern.compile(".*d")); // assertion will fail assertThat("Frodo").doesNotMatch(Pattern.compile("..o.o"));
- Parameters:
pattern
- the regular expression to which the actualCharSequence
is to be matched.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.AssertionError
- if the actualCharSequence
does not match the given regular expression.
-
isXmlEqualTo
public SELF isXmlEqualTo(CharSequence expectedXml)
Verifies that the actualCharSequence
is equal to the given XMLCharSequence
after both have been formatted the same way.Example :
String expectedXml = "<rings>\n" + " <bearer>\n" + " <name>Frodo</name>\n" + " <ring>\n" + " <name>one ring</name>\n" + " <createdBy>Sauron</createdBy>\n" + " </ring>\n" + " </bearer>\n" + "</rings>"; // No matter how your xml string is formated, isXmlEqualTo is able to compare it's content with another xml String. String oneLineXml = "<rings><bearer><name>Frodo</name><ring><name>one ring</name><createdBy>Sauron</createdBy></ring></bearer></rings>"; assertThat(oneLineXml).isXmlEqualTo(expectedXml); String xmlWithNewLine = "<rings>\n" + "<bearer> \n" + " <name>Frodo</name>\n" + " <ring>\n" + " <name>one ring</name>\n" + " <createdBy>Sauron</createdBy>\n" + " </ring>\n" + "</bearer>\n" + "</rings>"; assertThat(xmlWithNewLine).isXmlEqualTo(expectedXml); // You can compare it with oneLineXml assertThat(xmlWithNewLine).isXmlEqualTo(oneLineXml); // Tip : use isXmlEqualToContentOf assertion to compare your XML String with the content of an XML file : assertThat(oneLineXml).isXmlEqualToContentOf(new File("src/test/resources/formatted.xml"));
- Parameters:
expectedXml
- the XMLCharSequence
to which the actualCharSequence
is to be compared to.- Returns:
this
assertion object to chain other assertions.- Throws:
NullPointerException
- if the givenCharSequence
isnull
.AssertionError
- if the actualCharSequence
isnull
or is not the same XML as the given XMLCharSequence
.
-
isXmlEqualToContentOf
public SELF isXmlEqualToContentOf(File xmlFile)
Verifies that the actualCharSequence
is equal to the content of the given file.This is an handy shortcut that calls :
Example :isXmlEqualTo(contentOf(xmlFile))
// You can easily compare your XML String to the content of an XML file, whatever how formatted they are. String oneLineXml = "<rings><bearer><name>Frodo</name><ring><name>one ring</name><createdBy>Sauron</createdBy></ring></bearer></rings>"; assertThat(oneLineXml).isXmlEqualToContentOf(new File("src/test/resources/formatted.xml"));
- Parameters:
xmlFile
- the file to read the expected XML String to compare with actualCharSequence
- Returns:
this
assertion object to chain other assertions.- Throws:
NullPointerException
- if the givenFile
isnull
.AssertionError
- if the actualCharSequence
isnull
or is not the same XML as the content of givenFile
.
-
usingElementComparator
@Deprecated public final SELF usingElementComparator(Comparator<? super Character> customComparator)
Deprecated.Custom element Comparator is not supported for CharSequence comparison.Do not use this method.- Specified by:
usingElementComparator
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Parameters:
customComparator
- the comparator to use for incoming assertion checks.- Returns:
this
assertion object.- Throws:
UnsupportedOperationException
- if this method is called.
-
usingDefaultElementComparator
@Deprecated public final SELF usingDefaultElementComparator()
Deprecated.Custom element Comparator is not supported for CharSequence comparison.Do not use this method.- Specified by:
usingDefaultElementComparator
in interfaceEnumerableAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Returns:
this
assertion object.- Throws:
UnsupportedOperationException
- if this method is called.
-
usingComparator
public SELF usingComparator(Comparator<? super ACTUAL> customComparator)
Description copied from class:AbstractAssert
Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default comparison strategy. Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam);
- Specified by:
usingComparator
in interfaceAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Overrides:
usingComparator
in classAbstractAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Parameters:
customComparator
- the comparator to use for incoming assertion checks.- Returns:
this
assertion object.
-
usingDefaultComparator
public SELF usingDefaultComparator()
Description copied from class:AbstractAssert
Revert to standard comparison for incoming assertion checks.This method should be used to disable a custom comparison strategy set by calling
Assert.usingComparator(Comparator)
.- Specified by:
usingDefaultComparator
in interfaceAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Overrides:
usingDefaultComparator
in classAbstractAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Returns:
this
assertion object.
-
inHexadecimal
public SELF inHexadecimal()
Description copied from class:AbstractAssert
Use hexadecimal object representation instead of standard representation in error messages.It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned, it is thus impossible to find differences from the standard error message:
With standard message:
With Hexadecimal message:assertThat("µµµ").contains("μμμ"); java.lang.AssertionError: Expecting: <"µµµ"> to contain: <"μμμ">
assertThat("µµµ").inHexadecimal().contains("μμμ"); java.lang.AssertionError: Expecting: <"['00B5', '00B5', '00B5']"> to contain: <"['03BC', '03BC', '03BC']">
- Overrides:
inHexadecimal
in classAbstractAssert<SELF extends AbstractCharSequenceAssert<SELF,ACTUAL>,ACTUAL extends CharSequence>
- Returns:
this
assertion object.
-
inUnicode
public SELF inUnicode()
Use unicode character representation instead of standard representation in error messages.It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned, it is thus impossible to find differences from the standard error message:
With standard message:
With Hexadecimal message:assertThat("µµµ").contains("μμμ"); java.lang.AssertionError: Expecting: <"µµµ"> to contain: <"μμμ">
assertThat("µµµ").inUnicode().contains("μμμ"); java.lang.AssertionError: Expecting: <µµµ> to contain: <μμμ>
- Returns:
this
assertion object.
-
isEqualToIgnoringWhitespace
public SELF isEqualToIgnoringWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is equal to the given one, ignoring whitespace differencesExamples :
// assertions will pass assertThat("Game of Thrones").isEqualToIgnoringWhitespace("Game of Thrones") .isEqualToIgnoringWhitespace(" Game of Thrones ") .isEqualToIgnoringWhitespace(" Game of Thrones ") .isEqualToIgnoringWhitespace("Gameof Thrones") .isEqualToIgnoringWhitespace("Game of\tThrones") .isEqualToIgnoringWhitespace("GameofThrones"); // assertion will fail assertThat("Game of Thrones").isEqualToIgnoringWhitespace("Game OF Thrones");
This assertion behavior has changed in 2.8.0 to really ignore all whitespaces, the old behaviour has been kept in the better named
isEqualToNormalizingWhitespace(CharSequence)
.- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not equal ignoring whitespace differences to the given one.
-
isNotEqualToIgnoringWhitespace
public SELF isNotEqualToIgnoringWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is not equal to the given one, ignoring whitespace differences.Example :
// assertions will pass assertThat(" my\tfoo bar ").isNotEqualToIgnoringWhitespace("myfoo"); assertThat(" my\tfoo").isNotEqualToIgnoringWhitespace(" my bar"); // assertions will fail assertThat("my foo bar").isNotEqualToIgnoringWhitespace("my foo bar"); assertThat(" my foo bar ").isNotEqualToIgnoringWhitespace("my foo bar"); assertThat(" my foo bar ").isNotEqualToIgnoringWhitespace("my foo bar"); assertThat(" my\tfoo bar ").isNotEqualToIgnoringWhitespace(" my foo bar"); assertThat("my foo bar").isNotEqualToIgnoringWhitespace(" my foo bar ");
- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is equal ignoring whitespace differences to the given one.
-
isEqualToNormalizingWhitespace
public SELF isEqualToNormalizingWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is equal to the given one, after the whitespace of both strings has been normalized.
To be exact, the following rules are applied:- all leading and trailing whitespace of both actual and expected strings are ignored
- any remaining whitespace, appearing within either string, is collapsed to a single space before comparison
Example :
// assertions will pass assertThat("Game of Thrones").isEqualToNormalizingWhitespace("Game of Thrones") .isEqualToNormalizingWhitespace("Game of Thrones") .isEqualToNormalizingWhitespace("Game of Thrones") .isEqualToNormalizingWhitespace(" Game of Thrones ") .isEqualToNormalizingWhitespace(" Game of Thrones ") .isEqualToNormalizingWhitespace("Game of\tThrones") .isEqualToNormalizingWhitespace("Game of Thrones"); // assertions will fail assertThat("Game of Thrones").isEqualToNormalizingWhitespace("Game ofThrones"); assertThat("Game of Thrones").isEqualToNormalizingWhitespace("Gameo fThrones"); assertThat("Game of Thrones").isEqualToNormalizingWhitespace("Gameof Thrones");
- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not equal to the given one after whitespace has been normalized.- Since:
- 2.8.0 / 3.8.0
-
isNotEqualToNormalizingWhitespace
public SELF isNotEqualToNormalizingWhitespace(CharSequence expected)
Verifies that the actualCharSequence
is not equal to the given one, after the whitespace of both strings has been normalized.
To be exact, the following rules are applied:- all leading and trailing whitespace of both actual and expected strings are ignored
- any remaining whitespace, appearing within either string, is collapsed to a single space before comparison
Example :
// assertions will pass assertThat(" my\tfoo").isNotEqualToNormalizingWhitespace(" my bar"); assertThat(" my\tfoo bar ").isNotEqualToNormalizingWhitespace(" my foobar"); // assertions will fail assertThat("my foo bar").isNotEqualToNormalizingWhitespace("my foo bar"); assertThat(" my foo bar ").isNotEqualToNormalizingWhitespace("my foo bar"); assertThat(" my foo bar ").isNotEqualToNormalizingWhitespace("my foo bar"); assertThat(" my\tfoo bar ").isNotEqualToNormalizingWhitespace(" my foo bar"); assertThat("my foo bar").isNotEqualToNormalizingWhitespace(" my foo bar ");
- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is equal to the given one after whitespace has been normalized.- Since:
- 2.8.0 / 3.8.0
-
isSubstringOf
public SELF isSubstringOf(CharSequence sequence)
Verifies that the actualCharSequence
is a substring of the given one (opposite assertion ofcontains(CharSequence cs)
.Example :
// assertions will pass assertThat("Lego").isSubstringOf("Legolas"); assertThat("Legolas").isSubstringOf("Legolas"); // assertion will fail assertThat("Frodo").isSubstringOf("Frod");
- Parameters:
sequence
- the sequence that is expected to contain actual.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
is not a substring of the given parameter.
-
containsPattern
public SELF containsPattern(CharSequence regex)
Verifies that the actualCharSequence
contains the given regular expression.Example :
// assertion will pass assertThat("Frodo").containsPattern("Fr.d"); // assertion will fail assertThat("Frodo").containsPattern("Frodod");
- Parameters:
regex
- the regular expression to find in the actualCharSequence
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.PatternSyntaxException
- if the regular expression's syntax is invalid.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the given regular expression cannot be found in the actualCharSequence
.
-
containsPattern
public SELF containsPattern(Pattern pattern)
Verifies that the actualCharSequence
contains the given regular expression pattern.Example :
// assertion will pass assertThat("Frodo").containsPattern(Pattern.compile("Fr.d")); // assertion will fail assertThat("Frodo").containsPattern(Pattern.compile("Frodod"));
- Parameters:
pattern
- the regular expression to find in the actualCharSequence
.- Returns:
this
assertion object.- Throws:
NullPointerException
- if the given pattern isnull
.AssertionError
- if the actualCharSequence
isnull
.AssertionError
- if the given regular expression cannot be found in the actualCharSequence
.
-
isEqualToNormalizingNewlines
public SELF isEqualToNormalizingNewlines(CharSequence expected)
Verifies that the actualCharSequence
is equals to anotherCharSequence
after normalizing new line characters (i.e. '\r\n' == '\n').This assertion will succeed:
Whereas this assertion will fail:String bookName = "Lord of the Rings\r\n"; assertThat(bookName).isEqualToNormalizingNewlines("Lord of the Rings\n");
String singleLine = "\n"; assertThat(singleLine).isEqualToNormalizingNewlines("");
- Parameters:
expected
- the givenCharSequence
to compare the actualCharSequence
to.- Returns:
this
assertion object.- Throws:
AssertionError
- if the actualCharSequence
and the givenCharSequence
are different after new lines are normalized.- Since:
- 2.7.0 / 3.7.0
-
-