Class StringAssert


public class StringAssert extends GroupAssert<String>
Understands assertion methods for Strings. To create a new instance of this class use the method Assertions.assertThat(String).
  • Constructor Details

    • StringAssert

      protected StringAssert(String actual)
      Creates a new StringAssert.
      Parameters:
      actual - the target to verify.
  • Method Details

    • as

      public StringAssert as(String description)
      Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).as("name").isEqualTo("Frodo");
       

      Specified by:
      as in class GroupAssert<String>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public StringAssert describedAs(String description)
      Alias for GenericAssert.as(String), 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.

      For example:

       assertThat(val).describedAs("name").isEqualTo("Frodo");
       

      Specified by:
      describedAs in class GroupAssert<String>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • as

      public StringAssert as(Description description)
      Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
       

      Specified by:
      as in class GroupAssert<String>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public StringAssert describedAs(Description description)
      Alias for GenericAssert.as(Description), 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.

      For example:

       assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
       

      Specified by:
      describedAs in class GroupAssert<String>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • satisfies

      public StringAssert satisfies(Condition<String> condition)
      Verifies that the actual String satisfies the given condition.
      Specified by:
      satisfies in class GenericAssert<String>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual String does not satisfy the given condition.
      See Also:
    • doesNotSatisfy

      public StringAssert doesNotSatisfy(Condition<String> condition)
      Verifies that the actual String does not satisfy the given condition.
      Specified by:
      doesNotSatisfy in class GenericAssert<String>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual String satisfies the given condition.
      See Also:
    • is

      public StringAssert is(Condition<String> condition)
      Specified by:
      is in class GenericAssert<String>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual String does not satisfy the given condition.
      Since:
      1.2
    • isNot

      public StringAssert isNot(Condition<String> condition)
      Specified by:
      isNot in class GenericAssert<String>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual String satisfies the given condition.
      Since:
      1.2
    • isNotEmpty

      public StringAssert isNotEmpty()
      Verifies that the actual String contains at least on character.
      Specified by:
      isNotEmpty in class GroupAssert<String>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null or empty.
    • isEqualTo

      public StringAssert isEqualTo(String expected)
      Verifies that the actual String is equal to the given one.
      Specified by:
      isEqualTo in class GenericAssert<String>
      Parameters:
      expected - the given String to compare the actual String to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is not equal to the given one.
    • isEqualToIgnoringCase

      public StringAssert isEqualToIgnoringCase(String expected)
      Verifies that the actual String is equal to the given one ignoring case.
      Parameters:
      expected - the given String to compare the actual String to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String is not equal to the given one ignoring case.
    • isNotEqualTo

      public StringAssert isNotEqualTo(String other)
      Verifies that the actual String is not equal to the given one.
      Specified by:
      isNotEqualTo in class GenericAssert<String>
      Parameters:
      other - the given String to compare the actual String to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is equal to the given one.
    • isNotNull

      public StringAssert isNotNull()
      Verifies that the actual String is not null.
      Specified by:
      isNotNull in class GenericAssert<String>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
    • isNotSameAs

      public StringAssert isNotSameAs(String other)
      Verifies that the actual String is not the same as the given one.
      Specified by:
      isNotSameAs in class GenericAssert<String>
      Parameters:
      other - the given String to compare the actual String to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is the same as the given one.
    • isSameAs

      public StringAssert isSameAs(String expected)
      Verifies that the actual String is the same as the given one.
      Specified by:
      isSameAs in class GenericAssert<String>
      Parameters:
      expected - the given String to compare the actual String to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is not the same as the given one.
    • hasSize

      public StringAssert hasSize(int expected)
      Verifies that the number of characters in the actual String is equal to the given one.
      Specified by:
      hasSize in class GroupAssert<String>
      Parameters:
      expected - the expected number of characters in the actual String.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the number of characters of the actual String is not equal to the given one.
    • actualGroupSize

      protected int actualGroupSize()
      Returns the number of elements in the actual String.
      Specified by:
      actualGroupSize in class GroupAssert<String>
      Returns:
      the number of elements in the actual String.
    • contains

      public StringAssert contains(String expected)
      Verifies that the actual String contains the given one.
      Parameters:
      expected - the given String expected to be contained in the actual one.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String does not contain the given one.
    • endsWith

      public StringAssert endsWith(String expected)
      Verifies that the actual String ends with the given one.
      Parameters:
      expected - the given String expected to be at the end of the actual one.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String does not end with the given one.
    • startsWith

      public StringAssert startsWith(String expected)
      Verifies that the actual String starts with the given one.
      Parameters:
      expected - the given String expected to be at the beginning of the actual one.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String does not start with the given one.
    • excludes

      public StringAssert excludes(String s)
      Verifies that the actual String does not contains the given one.
      Parameters:
      s - the given String expected not to be contained in the actual one.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String does contain the given one.
    • matches

      public StringAssert matches(String regex)
      Verifies that the actual String 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 actual String is null.
      AssertionError - if the actual String does not match the given regular expression.
    • doesNotMatch

      public StringAssert doesNotMatch(String regex)
      Verifies that the actual String 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 actual String is null.
      AssertionError - if the actual String matches the given regular expression.
    • actual

      private String actual()
    • overridingErrorMessage

      public StringAssert overridingErrorMessage(String message)
      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 class GroupAssert<String>
      Parameters:
      message - the given error message, which will replace the default one.
      Returns:
      this assertion.
    • containsIgnoringCase

      public StringAssert containsIgnoringCase(String text)
      Verifies that the actual String contains the given text regardless of the case.
      Parameters:
      text - the given text.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String does not contain the given text.
      NullPointerException - if the given String is null.
      Since:
      1.3
    • doesNotContain

      public StringAssert doesNotContain(String text)
      Verifies that the actual String does not contain the given text.
      Parameters:
      text - the given text.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual String is null.
      AssertionError - if the actual String contains the given text.
      NullPointerException - if the given String is null.
      Since:
      1.3
    • validateNotNull

      private static void validateNotNull(String text)