Combining assertions

allOf

Given the following example:

<code lang="java">
  public void testAssert() {
    String s = "This is some text.";
    assertThat(
        "Assert a string starting with Tis and a string ending with ext.",
        s, Matchers.allOf(Matchers.startsWith("Tis"), Matchers
            .endsWith("ext.")));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert a string starting with Tis and a string 
    ending with ext.
  Expected: (a string starting with "Tis" and a string ending with "ext.")
    but: a string starting with "Tis" was "This is some text."
</code>

both

Given the following example:

<code lang="java">
  public void testAssert() {
    String s = "This is some text.";
    assertThat(
        "Assert a string starting with Tis and a string ending with ext.",
        s, Matchers.both(Matchers.startsWith("Tis")).and(
            Matchers.endsWith("ext.")));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert a string starting with Tis and a string 
    ending with ext.
  Expected: (a string starting with "Tis" and a string ending with "ext.")
    but: a string starting with "Tis" was "This is some text."
</code>

Leave a Reply

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