Package org.fest.assertions.api
Class StringAssert
- java.lang.Object
-
- org.fest.assertions.api.AbstractAssert<StringAssert,java.lang.String>
-
- org.fest.assertions.api.StringAssert
-
- All Implemented Interfaces:
Assert<StringAssert,java.lang.String>,Descriptable<StringAssert>,EnumerableAssert<StringAssert,java.lang.String>,ExtensionPoints<StringAssert,java.lang.String>
public class StringAssert extends AbstractAssert<StringAssert,java.lang.String> implements EnumerableAssert<StringAssert,java.lang.String>
Assertion methods forStrings.To create a new instance of this class, invoke
.Assertions.assertThat(String)- Author:
- Yvonne Wang, David DIDIER, Alex Ruiz, Joel Costigliola, Mikhail Mazursky, Nicolas Francois
-
-
Field Summary
-
Fields inherited from class org.fest.assertions.api.AbstractAssert
actual, myself
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedStringAssert(java.lang.String actual)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringAssertcontains(java.lang.String sequence)Verifies that the actualStringcontains the given sequence.StringAssertcontainsIgnoringCase(java.lang.String sequence)Verifies that the actualStringcontains the given sequence, ignoring case considerations.StringAssertcontainsOnlyOnce(java.lang.String sequence)Verifies that the actualStringcontains only once the given sequence.StringAssertdoesNotContain(java.lang.String sequence)Verifies that the actualStringdoes not contain the given sequence.StringAssertdoesNotMatch(java.lang.String regex)Verifies that the actualStringdoes not match the given regular expression.StringAssertdoesNotMatch(java.util.regex.Pattern pattern)Verifies that the actualStringdoes not match the given regular expression.StringAssertendsWith(java.lang.String suffix)Verifies that the actualStringends with the given suffix.StringAsserthasSameSizeAs(java.lang.Iterable<?> other)Verifies that the actual group has the same size as givenIterable.StringAsserthasSameSizeAs(java.lang.Object[] other)Verifies that the actual group has the same size as given array.StringAsserthasSize(int expected)Verifies that the number of values in the actual group is equal to the given one.voidisEmpty()Verifies that the actual group of values is empty.StringAssertisEqualToIgnoringCase(java.lang.String expected)Verifies that the actualStringis equal to the given one, ignoring case considerations.StringAssertisNotEmpty()Verifies that the actual group of values is not empty.voidisNullOrEmpty()Verifies that the actual group of values isnullor empty.StringAssertmatches(java.lang.String regex)Verifies that the actualStringmatches the given regular expression.StringAssertmatches(java.util.regex.Pattern pattern)Verifies that the actualStringmatches the given regular expression.StringAssertstartsWith(java.lang.String prefix)Verifies that the actualStringstarts with the given prefix.StringAssertusingComparator(java.util.Comparator<? super java.lang.String> customComparator)Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.StringAssertusingDefaultComparator()Revert to standard comparison for incoming assertion checks.StringAssertusingDefaultElementComparator()Revert to standard comparison for incoming assertion group element checks.StringAssertusingElementComparator(java.util.Comparator<? super java.lang.String> customComparator)Use given custom comparator instead of relying on actual type Aequalsmethod to compare group elements for incoming assertion checks.-
Methods inherited from class org.fest.assertions.api.AbstractAssert
as, as, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, getWritableAssertionInfo, has, hashCode, hasSameClassAs, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage
-
-
-
-
Method Detail
-
isNullOrEmpty
public void isNullOrEmpty()
Verifies that the actual group of values isnullor empty.- Specified by:
isNullOrEmptyin interfaceEnumerableAssert<StringAssert,java.lang.String>
-
isEmpty
public void isEmpty()
Verifies that the actual group of values is empty.- Specified by:
isEmptyin interfaceEnumerableAssert<StringAssert,java.lang.String>
-
isNotEmpty
public StringAssert isNotEmpty()
Verifies that the actual group of values is not empty.- Specified by:
isNotEmptyin interfaceEnumerableAssert<StringAssert,java.lang.String>- Returns:
thisassertion object.
-
hasSize
public StringAssert hasSize(int expected)
Verifies that the number of values in the actual group is equal to the given one.- Specified by:
hasSizein interfaceEnumerableAssert<StringAssert,java.lang.String>- Parameters:
expected- the expected number of values in the actual group.- Returns:
thisassertion object.
-
hasSameSizeAs
public StringAssert hasSameSizeAs(java.lang.Object[] other)
Verifies that the actual group has the same size as given array.- Specified by:
hasSameSizeAsin interfaceEnumerableAssert<StringAssert,java.lang.String>- Parameters:
other- the array to compare size with actual group.- Returns:
thisassertion object.
-
hasSameSizeAs
public StringAssert hasSameSizeAs(java.lang.Iterable<?> other)
Verifies that the actual group has the same size as givenIterable.- Specified by:
hasSameSizeAsin interfaceEnumerableAssert<StringAssert,java.lang.String>- Parameters:
other- theIterableto compare size with actual group.- Returns:
thisassertion object.
-
isEqualToIgnoringCase
public StringAssert isEqualToIgnoringCase(java.lang.String expected)
Verifies that the actualStringis equal to the given one, ignoring case considerations.- Parameters:
expected- the givenStringto compare the actualStringto.- Returns:
thisassertion object.- Throws:
java.lang.AssertionError- if the actualStringis not equal to the given one.
-
containsOnlyOnce
public StringAssert containsOnlyOnce(java.lang.String sequence)
Verifies that the actualStringcontains only once the given sequence.- Parameters:
sequence- the sequence to search for.- Returns:
thisassertion object.- Throws:
java.lang.AssertionError- if the actualStringdoes not contain the given one, or contain it multiple times.
-
contains
public StringAssert contains(java.lang.String sequence)
Verifies that the actualStringcontains the given sequence.- Parameters:
sequence- the sequence to search for.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given sequence isnull.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringdoes not contain the given one.
-
containsIgnoringCase
public StringAssert containsIgnoringCase(java.lang.String sequence)
Verifies that the actualStringcontains the given sequence, ignoring case considerations.- Parameters:
sequence- the sequence to search for.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given sequence isnull.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringdoes not contain the given one.
-
doesNotContain
public StringAssert doesNotContain(java.lang.String sequence)
Verifies that the actualStringdoes not contain the given sequence.- Parameters:
sequence- the sequence to search for.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given sequence isnull.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringcontains the given one.
-
startsWith
public StringAssert startsWith(java.lang.String prefix)
Verifies that the actualStringstarts with the given prefix.- Parameters:
prefix- the given prefix.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given prefix isnull.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringdoes not start with the given prefix.
-
endsWith
public StringAssert endsWith(java.lang.String suffix)
Verifies that the actualStringends with the given suffix.- Parameters:
suffix- the given suffix.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given suffix isnull.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringdoes not end with the given suffix.
-
matches
public StringAssert matches(java.lang.String regex)
Verifies that the actualStringmatches the given regular expression.- Parameters:
regex- the regular expression to which the actualStringis to be matched.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given pattern isnull.java.util.regex.PatternSyntaxException- if the regular expression's syntax is invalid.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringdoes not match the given regular expression.
-
doesNotMatch
public StringAssert doesNotMatch(java.lang.String regex)
Verifies that the actualStringdoes not match the given regular expression.- Parameters:
regex- the regular expression to which the actualStringis to be matched.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given pattern isnull.java.util.regex.PatternSyntaxException- if the regular expression's syntax is invalid.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringmatches the given regular expression.
-
matches
public StringAssert matches(java.util.regex.Pattern pattern)
Verifies that the actualStringmatches the given regular expression.- Parameters:
pattern- the regular expression to which the actualStringis to be matched.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given pattern isnull.java.lang.AssertionError- if the actualStringisnull.java.lang.AssertionError- if the actualStringdoes not match the given regular expression.
-
doesNotMatch
public StringAssert doesNotMatch(java.util.regex.Pattern pattern)
Verifies that the actualStringdoes not match the given regular expression.- Parameters:
pattern- the regular expression to which the actualStringis to be matched.- Returns:
thisassertion object.- Throws:
java.lang.NullPointerException- if the given pattern isnull.java.lang.AssertionError- if the actualStringdoes not match the given regular expression.
-
usingElementComparator
public StringAssert usingElementComparator(java.util.Comparator<? super java.lang.String> customComparator)
Use given custom comparator instead of relying on actual type Aequalsmethod to compare group elements 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 :
// compares invoices by payee assertThat(invoiceList).usingComparator(invoicePayeeComparator).isEqualTo(expectedInvoiceList). // compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator assertThat(invoiceList).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice) // as assertThat(invoiceList) creates a new assertion, it falls back to standard comparison strategy // based on Invoice's equal method to compare invoiceList elements to lowestInvoice. assertThat(invoiceList).contains(lowestInvoice). // standard comparison : the fellowshipOfTheRing includes Gandalf but not Sauron (believe me) ... assertThat(fellowshipOfTheRing).contains(gandalf) .doesNotContain(sauron); // ... but if we compare only races, Sauron is in fellowshipOfTheRing because he's a Maia like Gandalf. assertThat(fellowshipOfTheRing).usingElementComparator(raceComparator) .contains(sauron);- Specified by:
usingElementComparatorin interfaceEnumerableAssert<StringAssert,java.lang.String>- Parameters:
customComparator- the comparator to use for incoming assertion checks.- Returns:
thisassertion object.
-
usingDefaultElementComparator
public StringAssert usingDefaultElementComparator()
Revert to standard comparison for incoming assertion group element checks.This method should be used to disable a custom comparison strategy set by calling
EnumerableAssert.usingElementComparator(Comparator).- Specified by:
usingDefaultElementComparatorin interfaceEnumerableAssert<StringAssert,java.lang.String>- Returns:
thisassertion object.
-
usingComparator
public StringAssert usingComparator(java.util.Comparator<? super java.lang.String> customComparator)
Description copied from class:AbstractAssertUse 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:
usingComparatorin interfaceAssert<StringAssert,java.lang.String>- Overrides:
usingComparatorin classAbstractAssert<StringAssert,java.lang.String>- Parameters:
customComparator- the comparator to use for incoming assertion checks.- Returns:
thisassertion object.
-
usingDefaultComparator
public StringAssert usingDefaultComparator()
Description copied from class:AbstractAssertRevert 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:
usingDefaultComparatorin interfaceAssert<StringAssert,java.lang.String>- Overrides:
usingDefaultComparatorin classAbstractAssert<StringAssert,java.lang.String>- Returns:
thisassertion object.
-
-