Package org.fest.assertions
Class StringAssert
Understands assertion methods for
String
s. To create a new instance of this class use the
method Assertions.assertThat(String)
.-
Field Summary
Fields inherited from class org.fest.assertions.GenericAssert
actual
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate String
actual()
protected int
Returns the number of elements in the actualString
.Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails.AssertionError
as
(Description description) Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails.AssertionError
Verifies that the actualString
contains the given one.containsIgnoringCase
(String text) Verifies that the actualString
contains the given text regardless of the case.describedAs
(String description) Alias for
, since "as" is a keyword in Groovy.GenericAssert.as(String)
describedAs
(Description description) Alias for
, since "as" is a keyword in Groovy.GenericAssert.as(Description)
doesNotContain
(String text) Verifies that the actualString
does not contain the given text.doesNotMatch
(String regex) Verifies that the actualString
does not match the given one.doesNotSatisfy
(Condition<String> condition) Verifies that the actualString
does not satisfy the given condition.Verifies that the actualString
ends with the given one.Verifies that the actualString
does not contains the given one.hasSize
(int expected) Verifies that the number of characters in the actualString
is equal to the given one.Alias for
.satisfies(Condition)
Verifies that the actualString
is equal to the given one.isEqualToIgnoringCase
(String expected) Verifies that the actualString
is equal to the given one ignoring case.Alias for
.doesNotSatisfy(Condition)
Verifies that the actualString
contains at least on character.isNotEqualTo
(String other) Verifies that the actualString
is not equal to the given one.Verifies that the actualString
is notnull
.isNotSameAs
(String other) Verifies that the actualString
is not the same as the given one.Verifies that the actualString
is the same as the given one.Verifies that the actualString
matches the given one.overridingErrorMessage
(String message) Replaces the default message displayed in case of a failure with the given one.Verifies that the actualString
satisfies the given condition.startsWith
(String expected) Verifies that the actualString
starts with the given one.private static void
validateNotNull
(String text) Methods inherited from class org.fest.assertions.GroupAssert
assertHasSize, assertIsNotEmpty, isEmpty, isNullOrEmpty
Methods inherited from class org.fest.assertions.GenericAssert
assertDoesNotSatisfy, assertEqualTo, assertIs, assertIsNot, assertNotEqualTo, assertNotNull, assertNotSameAs, assertSameAs, assertSatisfies, isNull
Methods inherited from class org.fest.assertions.Assert
customErrorMessage, description, description, description, equals, fail, fail, failIfCustomMessageIsSet, failIfCustomMessageIsSet, failure, formattedErrorMessage, hashCode, rawDescription, replaceDefaultErrorMessagesWith
-
Constructor Details
-
StringAssert
Creates a newStringAssert
.- Parameters:
actual
- the target to verify.
-
-
Method Details
-
as
Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.AssertionError
For example:
assertThat(val).as("name").isEqualTo("Frodo");
- Specified by:
as
in classGroupAssert<String>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
describedAs
Alias for
, since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.GenericAssert.as(String)
For example:
assertThat(val).describedAs("name").isEqualTo("Frodo");
- Specified by:
describedAs
in classGroupAssert<String>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
as
Sets the description of the actual value, to be used in as message of any
thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.AssertionError
For example:
assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
- Specified by:
as
in classGroupAssert<String>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
describedAs
Alias for
, since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.GenericAssert.as(Description)
For example:
assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
- Specified by:
describedAs
in classGroupAssert<String>
- Parameters:
description
- the description of the actual value.- Returns:
- this assertion object.
-
satisfies
Verifies that the actualString
satisfies the given condition.- Specified by:
satisfies
in classGenericAssert<String>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualString
does not satisfy the given condition.- See Also:
-
doesNotSatisfy
Verifies that the actualString
does not satisfy the given condition.- Specified by:
doesNotSatisfy
in classGenericAssert<String>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualString
satisfies the given condition.- See Also:
-
is
Alias for
.satisfies(Condition)
- Specified by:
is
in classGenericAssert<String>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualString
does not satisfy the given condition.- Since:
- 1.2
-
isNot
Alias for
.doesNotSatisfy(Condition)
- Specified by:
isNot
in classGenericAssert<String>
- Parameters:
condition
- the given condition.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if the given condition isnull
.AssertionError
- if the actualString
satisfies the given condition.- Since:
- 1.2
-
isNotEmpty
Verifies that the actualString
contains at least on character.- Specified by:
isNotEmpty
in classGroupAssert<String>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
or empty.
-
isEqualTo
Verifies that the actualString
is equal to the given one.- Specified by:
isEqualTo
in classGenericAssert<String>
- Parameters:
expected
- the givenString
to compare the actualString
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
is not equal to the given one.
-
isEqualToIgnoringCase
Verifies that the actualString
is equal to the given one ignoring case.- Parameters:
expected
- the givenString
to compare the actualString
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
is not equal to the given one ignoring case.
-
isNotEqualTo
Verifies that the actualString
is not equal to the given one.- Specified by:
isNotEqualTo
in classGenericAssert<String>
- Parameters:
other
- the givenString
to compare the actualString
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
is equal to the given one.
-
isNotNull
Verifies that the actualString
is notnull
.- Specified by:
isNotNull
in classGenericAssert<String>
- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.
-
isNotSameAs
Verifies that the actualString
is not the same as the given one.- Specified by:
isNotSameAs
in classGenericAssert<String>
- Parameters:
other
- the givenString
to compare the actualString
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
is the same as the given one.
-
isSameAs
Verifies that the actualString
is the same as the given one.- Specified by:
isSameAs
in classGenericAssert<String>
- Parameters:
expected
- the givenString
to compare the actualString
to.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
is not the same as the given one.
-
hasSize
Verifies that the number of characters in the actualString
is equal to the given one.- Specified by:
hasSize
in classGroupAssert<String>
- Parameters:
expected
- the expected number of characters in the actualString
.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the number of characters of the actualString
is not equal to the given one.
-
actualGroupSize
protected int actualGroupSize()Returns the number of elements in the actualString
.- Specified by:
actualGroupSize
in classGroupAssert<String>
- Returns:
- the number of elements in the actual
String
.
-
contains
Verifies that the actualString
contains the given one.- Parameters:
expected
- the givenString
expected to be contained in the actual one.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
does not contain the given one.
-
endsWith
Verifies that the actualString
ends with the given one.- Parameters:
expected
- the givenString
expected to be at the end of the actual one.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
does not end with the given one.
-
startsWith
Verifies that the actualString
starts with the given one.- Parameters:
expected
- the givenString
expected to be at the beginning of the actual one.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
does not start with the given one.
-
excludes
Verifies that the actualString
does not contains the given one.- Parameters:
s
- the givenString
expected not to be contained in the actual one.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
does contain the given one.
-
matches
Verifies that the actualString
matches the given one.- Parameters:
regex
- the given regular expression expected to be matched by the actual one.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
does not match the given regular expression.
-
doesNotMatch
Verifies that the actualString
does not match the given one.- Parameters:
regex
- the given regular expression expected not to be matched by the actual one.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
matches the given regular expression.
-
actual
-
overridingErrorMessage
Replaces the default message displayed in case of a failure with the given one.For example, the following assertion:
assertThat("Hello").isEqualTo("Bye");
will fail with the default message "expected:<'[Bye]'> but was:<'[Hello]'>."We can replace this message with our own:
assertThat("Hello").overridingErrorMessage("'Hello' should be equal to 'Bye'").isEqualTo("Bye");
in this case, the assertion will fail showing the message "'Hello' should be equal to 'Bye'".- Specified by:
overridingErrorMessage
in classGroupAssert<String>
- Parameters:
message
- the given error message, which will replace the default one.- Returns:
- this assertion.
-
containsIgnoringCase
Verifies that the actualString
contains the given text regardless of the case.- Parameters:
text
- the given text.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
does not contain the given text.NullPointerException
- if the givenString
isnull
.- Since:
- 1.3
-
doesNotContain
Verifies that the actualString
does not contain the given text.- Parameters:
text
- the given text.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualString
isnull
.AssertionError
- if the actualString
contains the given text.NullPointerException
- if the givenString
isnull
.- Since:
- 1.3
-
validateNotNull
-