Class AbstractStringAssert<SELF extends AbstractStringAssert<SELF>>
- All Implemented Interfaces:
Assert<SELF,,String> Descriptable<SELF>,EnumerableAssert<SELF,,Character> ExtensionPoints<SELF,String>
- Direct Known Subclasses:
StringAssert
-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDecodes the actual value as a Base64 encoded string, the decoded bytes becoming the new array under test.Parses the actual value as boolean, the parsed boolean becoming the new value under test.asByte()Parses the actual value as byte, using radix 10, the parsed byte becoming the new value under test.asDouble()Parses the actual value as double, the parsed double becoming the new value under test.asFloat()Parses the actual value as float, the parsed float becoming the new value under test.asInt()Parses the actual value as integer, using radix 10, the parsed integer becoming the new value under test.asLong()Parses the actual value as long, using radix 10, the parsed long becoming the new value under test.asShort()Parses the actual value as short, using radix 10, the parsed short becoming the new value under test.bytes()Encodes the actual value as byte array using the platform's default charset, the encoded byte array becoming the new value under test.Encodes the actual value as byte array using a specificCharset, the encoded byte array becoming the new value under test.Encodes the actual value as byte array using a specificCharset, the encoded byte array becoming the new value under test.Deprecated.isBase64()Verifies that the actual value is a valid Base64 encoded string.Verifies that the actual value is in [start, end] range (start included, end included) according toString.compareTo(String).Verifies that the actual value is equal to expected.Verifies that the actual value is equal to expected build usingString.format(String stringTemplate, Object...args).isGreaterThan(String other) Verifies that the actual value is greater than the givenStringaccording toString.compareTo(String).isGreaterThanOrEqualTo(String other) Verifies that the actual value is greater than or equal to the givenStringaccording toString.compareTo(String).isLessThan(String other) Verifies that the actual value is less than the givenStringaccording toString.compareTo(String).isLessThanOrEqualTo(String other) Verifies that the actual value is less than or equal to the givenStringaccording toString.compareTo(String).isStrictlyBetween(String startExclusive, String endExclusive) Verifies that the actual value is strictly in ]start, end[ range (start excluded, end excluded) according toString.compareTo(String).usingComparator(Comparator<? super String> customComparator) Use the given custom comparator instead of relying onStringnatural comparator for the incoming assertions.usingComparator(Comparator<? super String> customComparator, String customComparatorDescription) Use the given custom comparator instead of relying onStringnatural comparator for the incoming assertions.Revert to standard comparison for the incoming assertion checks.Methods inherited from class org.assertj.core.api.AbstractCharSequenceAssert
contains, contains, containsAnyOf, containsIgnoringCase, containsIgnoringNewLines, containsIgnoringWhitespaces, containsOnlyDigits, containsOnlyOnce, containsOnlyWhitespaces, containsPattern, containsPattern, containsPatternSatisfying, containsPatternSatisfying, containsSequence, containsSequence, containsSubsequence, containsSubsequence, containsWhitespaces, doesNotContain, doesNotContain, doesNotContainAnyWhitespaces, doesNotContainIgnoringCase, doesNotContainOnlyWhitespaces, doesNotContainPattern, doesNotContainPattern, doesNotEndWith, doesNotEndWithIgnoringCase, doesNotEndWithWhitespaces, doesNotMatch, doesNotMatch, doesNotStartWith, doesNotStartWithIgnoringCase, doesNotStartWithWhitespaces, endsWith, endsWithIgnoringCase, hasLineCount, hasSameSizeAs, hasSameSizeAs, hasSameSizeAs, hasSize, hasSizeBetween, hasSizeGreaterThan, hasSizeGreaterThanOrEqualTo, hasSizeLessThan, hasSizeLessThanOrEqualTo, inBinary, inHexadecimal, inUnicode, isAlphabetic, isAlphanumeric, isASCII, isBlank, isEmpty, isEqualToIgnoringCase, isEqualToIgnoringNewLines, isEqualToIgnoringWhitespace, isEqualToNormalizingNewlines, isEqualToNormalizingPunctuationAndWhitespace, isEqualToNormalizingUnicode, isEqualToNormalizingWhitespace, isHexadecimal, isJavaBlank, isLowerCase, isMixedCase, isNotBlank, isNotEmpty, isNotEqualToIgnoringCase, isNotEqualToIgnoringWhitespace, isNotEqualToNormalizingWhitespace, isNotJavaBlank, isNullOrEmpty, isPrintable, isSubstringOf, isUpperCase, isVisible, isXmlEqualTo, isXmlEqualToContentOf, matches, matches, matchesSatisfying, matchesSatisfying, startsWith, startsWithIgnoringCase, usingDefaultElementComparator, usingElementComparatorMethods inherited from class org.assertj.core.api.AbstractAssert
actual, areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, doesNotMatch, doesNotMatch, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingEquals, usingEquals, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnErrorMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.assertj.core.api.Descriptable
as, as, as, describedAs, describedAs
-
Constructor Details
-
AbstractStringAssert
-
-
Method Details
-
isLessThan
Verifies that the actual value is less than the givenStringaccording toString.compareTo(String).Note that it is possible to change the comparison strategy with
usingComparator.Examples:
// assertions succeed assertThat("abc").isLessThan("bcd") .isLessThan("b") .isLessThan("abca") .usingComparator(String.CASE_INSENSITIVE_ORDER) .isLessThan("BCD"); // assertions fail assertThat("abc").isLessThan("ab"); assertThat("abc").isLessThan("abc"); assertThat("abc").isLessThan("ABC");- Parameters:
other- theStringto compare the actual value to.- Returns:
thisassertion object.- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is greater than or equal to the given one.- Since:
- 3.11.0
-
isLessThanOrEqualTo
Verifies that the actual value is less than or equal to the givenStringaccording toString.compareTo(String).Note that it is possible to change the comparison strategy with
usingComparator.Examples:
// assertions succeed assertThat("abc").isLessThanOrEqualTo("bcd") .isLessThanOrEqualTo("abc") .isLessThanOrEqualTo("b") .isLessThanOrEqualTo("abca") .usingComparator(String.CASE_INSENSITIVE_ORDER) .isLessThanOrEqualTo("ABC"); // assertions fail assertThat("abc").isLessThanOrEqualTo("ab"); assertThat("abc").isLessThanOrEqualTo("ABC");- Parameters:
other- theStringto compare the actual value to.- Returns:
thisassertion object.- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is greater than the given one.- Since:
- 3.11.0
-
isGreaterThan
Verifies that the actual value is greater than the givenStringaccording toString.compareTo(String).Note that it is possible to change the comparison strategy with
usingComparator.Examples:
// assertions succeed assertThat("xyz").isGreaterThan("abc") .isGreaterThan("xy") .isGreaterThan("ABC"); assertThat("XYZ").usingComparator(String.CASE_INSENSITIVE_ORDER) .isGreaterThan("abc"); // assertions fail assertThat("xyz").isGreaterThan("xyzz"); assertThat("xyz").isGreaterThan("xyz"); assertThat("xyz").isGreaterThan("z");- Parameters:
other- theStringto compare the actual value to.- Returns:
thisassertion object.- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is less than or equal to the given one.- Since:
- 3.11.0
-
isGreaterThanOrEqualTo
Verifies that the actual value is greater than or equal to the givenStringaccording toString.compareTo(String).Note that it is possible to change the comparison strategy with
usingComparator.Examples:
// assertions succeed assertThat("xyz").isGreaterThanOrEqualTo("abc") .isGreaterThanOrEqualTo("xyz") .isGreaterThanOrEqualTo("xy") .isGreaterThanOrEqualTo("ABC"); assertThat("XYZ").usingComparator(String.CASE_INSENSITIVE_ORDER) .isGreaterThanOrEqualTo("abc"); // assertions fail assertThat("xyz").isGreaterThanOrEqualTo("xyzz"); assertThat("xyz").isGreaterThanOrEqualTo("z");- Parameters:
other- theStringto compare the actual value to.- Returns:
thisassertion object.- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is less than the given one.- Since:
- 3.11.0
-
isBetween
Verifies that the actual value is in [start, end] range (start included, end included) according toString.compareTo(String).Note that it is possible to change the comparison strategy with
usingComparator.Examples:
// assertions succeed assertThat("ab").isBetween("aa", "ac") .isBetween("ab", "ac") .isBetween("aa", "ab") .isBetween("ab", "ab") .isBetween("a", "c") .usingComparator(String.CASE_INSENSITIVE_ORDER) .isBetween("AA", "AC"); // assertions fail assertThat("ab").isBetween("ac", "bc"); assertThat("ab").isBetween("abc", "ac");- Parameters:
startInclusive- the start value (inclusive), expected not to be null.endInclusive- the end value (inclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actual value isnull.NullPointerException- if start value isnull.NullPointerException- if end value isnull.AssertionError- if the actual value is not in the [start, end] range.- Since:
- 3.11.0
-
isStrictlyBetween
Verifies that the actual value is strictly in ]start, end[ range (start excluded, end excluded) according toString.compareTo(String).Note that it is possible to change the comparison strategy with
usingComparator.Examples:
// assertions succeed assertThat("ab").isStrictlyBetween("aa", "ac") .isStrictlyBetween("a", "c") .usingComparator(String.CASE_INSENSITIVE_ORDER) .isStrictlyBetween("AA", "AC"); // assertions fail assertThat("ab").isStrictlyBetween("ac", "bc"); assertThat("ab").isStrictlyBetween("ab", "ac"); assertThat("ab").isStrictlyBetween("aa", "ab"); assertThat("ab").isStrictlyBetween("abc", "ac");- Parameters:
startExclusive- the start value (exclusive), expected not to be null.endExclusive- the end value (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actual value isnull.NullPointerException- if start value isnull.NullPointerException- if end value isnull.AssertionError- if the actual value is not in ]start, end[ range.- Since:
- 3.11.0
-
isBase64
Verifies that the actual value is a valid Base64 encoded string.Examples:
// assertion succeeds assertThat("QXNzZXJ0Sg==").isBase64(); // assertion succeeds even without padding as it is optional by specification assertThat("QXNzZXJ0Sg").isBase64(); // assertion fails as it has invalid Base64 characters assertThat("inv@lid").isBase64();- Returns:
- this assertion object.
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is not a valid Base64 encoded string.- Since:
- 3.16.0
-
asBase64Decoded
Decodes the actual value as a Base64 encoded string, the decoded bytes becoming the new array under test.Examples:
// assertion succeeds assertThat("QXNzZXJ0Sg==").asBase64Decoded().containsExactly("AssertJ".getBytes()); // assertion succeeds even without padding as it is optional by specification assertThat("QXNzZXJ0Sg").asBase64Decoded().containsExactly("AssertJ".getBytes()); // assertion fails as it has invalid Base64 characters assertThat("inv@lid").asBase64Decoded();- Returns:
- a new
ByteArrayAssertinstance whose array under test is the result of the decoding. - Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is not a valid Base64 encoded string.- Since:
- 3.22.0
-
decodedAsBase64
Deprecated.useasBase64Decoded()instead.Decodes the actual value as a Base64 encoded string, the decoded bytes becoming the new array under test.
Examples:
// assertion succeeds assertThat("QXNzZXJ0Sg==").decodedAsBase64().containsExactly("AssertJ".getBytes()); // assertion succeeds even without padding as it is optional by specification assertThat("QXNzZXJ0Sg").decodedAsBase64().containsExactly("AssertJ".getBytes()); // assertion fails as it has invalid Base64 characters assertThat("inv@lid").decodedAsBase64();- Returns:
- a new
ByteArrayAssertinstance whose array under test is the result of the decoding. - Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is not a valid Base64 encoded string.- Since:
- 3.16.0
-
usingComparator
Use the given custom comparator instead of relying onStringnatural comparator for the incoming assertions.The custom comparator is bound to an assertion instance, meaning that if a new assertion instance is created it is forgotten and the default (
Stringnatural comparator) is used.Examples :
// assertions succeed assertThat("abc").usingComparator(String.CASE_INSENSITIVE_ORDER) .isEqualTo("Abc") .isEqualTo("ABC"); // assertion fails as it relies on String natural comparator assertThat("abc").isEqualTo("ABC");- Specified by:
usingComparatorin interfaceAssert<SELF extends AbstractStringAssert<SELF>,String> - Overrides:
usingComparatorin classAbstractCharSequenceAssert<SELF extends AbstractStringAssert<SELF>,String> - Parameters:
customComparator- the comparator to use for the incoming assertions.- Returns:
thisassertion object.- Throws:
NullPointerException- if the given comparator isnull.
-
usingComparator
public SELF usingComparator(Comparator<? super String> customComparator, String customComparatorDescription) Use the given custom comparator instead of relying onStringnatural comparator for the incoming assertions.The custom comparator is bound to an assertion instance, meaning that if a new assertion instance is created it is forgotten and the default (
Stringnatural comparator) is used.Examples :
// assertions succeed assertThat("abc").usingComparator(String.CASE_INSENSITIVE_ORDER, "String case insensitive comparator") .isEqualTo("Abc") .isEqualTo("ABC"); // assertion fails as it relies on String natural comparator assertThat("abc").isEqualTo("ABC");- Specified by:
usingComparatorin interfaceAssert<SELF extends AbstractStringAssert<SELF>,String> - Overrides:
usingComparatorin classAbstractCharSequenceAssert<SELF extends AbstractStringAssert<SELF>,String> - Parameters:
customComparator- the comparator to use for the incoming assertions.customComparatorDescription- comparator description to be used in assertion error messages- Returns:
thisassertion object.- Throws:
NullPointerException- if the given comparator isnull.
-
usingDefaultComparator
Description copied from class:AbstractAssertRevert to standard comparison for the incoming assertion checks.This method should be used to disable a custom comparison strategy set by calling
usingComparator.- Specified by:
usingDefaultComparatorin interfaceAssert<SELF extends AbstractStringAssert<SELF>,String> - Overrides:
usingDefaultComparatorin classAbstractCharSequenceAssert<SELF extends AbstractStringAssert<SELF>,String> - Returns:
thisassertion object.
-
isEqualTo
Verifies that the actual value is equal to expected build usingString.format(String stringTemplate, Object...args).Note that for this assertion to be called, you must use a format template with parameters otherwise
AbstractAssert.isEqualTo(Object)is called which does not perform any formatting. For example, if you only use%nin the template they won't be replaced.Examples:
// assertion succeeds assertThat("R2D2").isEqualTo("%d%s%d%s", "R", 2, "D", 2); // assertion fails assertThat("C6PO").isEqualTo("%d%s%d%s", "R", 2, "D", 2); // assertion fails withNullPointerExceptionassertThat("1,A,2").isEqualTo(null, 1, "A", 2); // assertion fails withIllegalFormatExceptionassertThat("1").isEqualTo("%s%s", 1);- Parameters:
expectedStringTemplate- the format template used to build the expected String.args- the arguments referenced by the format specifiers in the format string.- Returns:
- this assertion object.
- Throws:
NullPointerException- if stringTemplate parameter isnull.AssertionError- if the actual value isnullas the template you provide must not benull.IllegalFormatException- as inString.format(String, Object...), see Details section of the formatter class specification.- Since:
- 3.12.0
-
isEqualTo
Verifies that the actual value is equal to expected.This method needs to be overridden because otherwise
isEqualTo(String, Object...)is called from tests in Kotlin without args which breaks whenever the is%in the string.- Parameters:
expected- the givenStringto compare the actual to.- Returns:
- this assertion object.
- Since:
- 3.13.0
- See Also:
-
asBoolean
Parses the actual value as boolean, the parsed boolean becoming the new value under test.Note that only when the string is equal to the string "true", ignoring case, and can have leading and trailing space, the parsed value will be
true. Otherwise, the value will befalse.Examples:
assertThat("truE").asBoolean().isTrue(); assertThat("false").asBoolean().isFalse(); assertThat("foo bar").asBoolean().isFalse(); assertThat((String) null).asBoolean().isFalse();- Returns:
- a new
BooleanAssertinstance whose value under test is the result of the parse. - Since:
- 3.25.0
-
asByte
Parses the actual value as byte, using radix 10, the parsed byte becoming the new value under test.Examples:
// assertion succeeds: assertThat("127").asByte().isEqualTo((byte) 127); // assertions fail as the actual value is null or not a valid byte assertThat((String) null).asByte(); assertThat("1L").asByte();- Returns:
- a new
ByteAssertinstance whose value under test is the result of the parse. - Throws:
AssertionError- if the actual value is null or not a valid byte.- Since:
- 3.25.0
-
bytes
Encodes the actual value as byte array using the platform's default charset, the encoded byte array becoming the new value under test.Examples:
assertThat("abc").bytes() .isEqualTo(new byte[] {'a', 'b', 'c'}); assertThat("").bytes() .isEmpty();- Returns:
- a new
AbstractByteArrayAssertinstance whose value under test is the result of parsing the string. - Throws:
AssertionError- if the actual string isnull.- Since:
- 3.26.0
-
bytes
Encodes the actual value as byte array using a specificCharset, the encoded byte array becoming the new value under test.Examples:
assertThat("abc").bytes(StandardCharsets.US_ASCII) .isEqualTo("abc".getBytes(StandardCharsets.US_ASCII)); assertThat("").bytes(StandardCharsets.US_ASCII) .isEmpty();- Parameters:
charset- theCharsetto be used to encode the string.- Returns:
- a new
AbstractByteArrayAssertinstance whose value under test is the result of parsing the string. - Throws:
NullPointerException- if charset parameter isnull.AssertionError- if the actual string isnull.- Since:
- 3.26.0
-
bytes
Encodes the actual value as byte array using a specificCharset, the encoded byte array becoming the new value under test.Examples:
assertThat("abc").bytes(StandardCharsets.US_ASCII) .isEqualTo("abc".getBytes(StandardCharsets.US_ASCII)); assertThat("").bytes(StandardCharsets.US_ASCII) .isEmpty();- Parameters:
charsetName- the Charset to be used to encode the string.- Returns:
- a new
AbstractByteArrayAssertinstance whose value under test is the result of parsing the string. - Throws:
NullPointerException- if named charset parameter isnull.AssertionError- if the actual string isnullor if the named charset parameter is not supported.- Since:
- 3.26.0
-
asShort
Parses the actual value as short, using radix 10, the parsed short becoming the new value under test.Examples:
// assertion succeeds: assertThat("32767").asShort().isEqualTo((short) 32767); // assertions fail as the actual value is null or not a valid short: assertThat((String) null).asShort(); assertThat("-32769").asShort();- Returns:
- a new
ShortAssertinstance whose value under test is the result of the parse. - Throws:
AssertionError- if the actual value is null or not a valid short.- Since:
- 3.25.0
-
asInt
Parses the actual value as integer, using radix 10, the parsed integer becoming the new value under test.Examples:
// assertion succeeds: assertThat("2147483647").asInt().isEqualTo(2147483647); // assertions fail as the actual value is null or not a valid int: assertThat((String) null).asInt(); assertThat("1e100").asInt();- Returns:
- a new
IntegerAssertinstance whose value under test is the result of the parse. - Throws:
AssertionError- if the actual value is null or not a valid int.- Since:
- 3.25.0
-
asLong
Parses the actual value as long, using radix 10, the parsed long becoming the new value under test.Examples:
// assertion succeeds: assertThat("1").asLong().isEqualTo(1L); // assertions fail as the actual value is null or not a long: assertThat((String) null).asLong(); assertThat("1e100").asLong();- Returns:
- a new
LongAssertinstance whose value under test is the result of the parse. - Throws:
AssertionError- if the actual value is null or not a valid long.- Since:
- 3.25.0
-
asFloat
Parses the actual value as float, the parsed float becoming the new value under test.Examples:
// assertion succeeds: assertThat("1.2f").asFloat().isCloseTo(1.2f, withinPercentage(0.01)); // assertions fail as the actual value is null or not a float: assertThat((String) null).asFloat(); assertThat("foo").asFloat();- Returns:
- a new
FloatAssertinstance whose value under test is the result of the parse. - Throws:
AssertionError- if the actual value is null or not a parseable float.- Since:
- 3.25.0
-
asDouble
Parses the actual value as double, the parsed double becoming the new value under test.Examples:
// assertion succeeds: assertThat("1.2e308").asDouble().isCloseTo(1.2e308, withinPercentage(0.001)); // assertions fail as the actual value is null or not a double: assertThat((String) null).asDouble(); assertThat("foo").asDouble();- Returns:
- a new
DoubleAssertinstance whose value under test is the result of the parse. - Throws:
AssertionError- if the actual value is null or not a parseable double.- Since:
- 3.25.0
-
asBase64Decoded()instead.