Assert collections

hasItems

Given the following example:

<code lang="java">
  public void testAssert() {
    List<String> list = new ArrayList<String>();
    list.add("test");
    list.add("webdriver");
    assertThat("Assert that the list contains: tedst", list,
        Matchers.hasItems("tedst"));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert that the list contains: tedst
  Expected: (a collection containing "tedst")
    but: a collection containing "tedst" was "test", was "webdriver"
</code>

hasSize

Given the following example:

<code lang="java">
  public void testAssert() {
    List<String> list = new ArrayList<String>();
    list.add("test");
    list.add("webdriver");
    assertThat("Assert that the list has size of 3", list,
        Matchers.hasSize(3));
  }
</code>

Returns the following output when the assertion fails:

<code lang="console">
  java.lang.AssertionError: Assert that the list has size of 3
  Expected: a collection with size <3>
    but: collection size was <2>
</code>

Leave a Reply

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