Assert text

Rate this post

equalTo

Given the following example:

<code lang="java">
  public void testAssert() {
    String s = "This is some text.";
    assertThat("Assert s to match the exact text", s, Matchers
        .equalTo("Tis is some text."));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert s to match the exact text
  Expected: is "Tis is some text."
    but: was "This is some text."
</code>

startsWith

Given the following example:

<code lang="java">
  public void testAssert() {
    String s = "This is some text.";
    assertThat("Assert s to match the beginning of the string",
        s, Matchers.startsWith("Ths"));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert s to match the beginning of the string
  Expected: a string starting with "Ths"
    but: was "This is some text."
</code>

endsWith

Given the following example:

<code lang="java">
  public void testAssert() {
    String s = "This is some text.";
    assertThat("Assert s to match the end of the string", s,
        Matchers.endsWith("x."));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert s to match the end of the string
  Expected: a string ending with "x."
    but: was "This is some text."
</code>

containsString

Given the following example:

<code lang="java">
  public void testAssert() {
    String s = "This is some text.";
    assertThat("Assert s to check if string contains", s,
        Matchers.containsString("sme"));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert s to check if string contains
  Expected: a string containing "sme"
    but: was "This is some text."
</code>

Leave a Reply

Your email address will not be published. Required fields are marked *