Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure what version of JUnit you're using, but recent ones have an <code>assertThat</code> method which takes a <a href="http://code.google.com/p/hamcrest/wiki/Tutorial" rel="noreferrer">Hamcrest Matcher</a> as an argument. They're composable so you can build up complex assertions about a collection.</p> <p>For instance, if you wanted to assert that a collection <code>A</code> contained every element in collection <code>B</code>, you could write:</p> <pre><code>import static org.junit.Assert.*; import static org.junit.matchers.JUnitMatchers.*; import static org.hamcrest.core.IsCollectionContaining.*; import static org.hamcrest.collection.IsCollectionWithSize.*; import org.hamcrest.beans.SamePropertyValuesAs; public class CollectionTests { /* * Tests that a contains every element in b (using the equals() * method of each element) and that a has the same size as b. */ @Test public void test() { Collection&lt;Foo&gt; a = doSomething(); Collection&lt;Foo&gt; b = expectedAnswer; assertThat(a, both(hasItems(b)).and(hasSize(b.size()))); } /* * Tests that a contains every element in b (using introspection * to compare bean properties) and that a has the same size as b. */ @Test public void testBeans() { Collection&lt;Foo&gt; a = doSomething(); Collection&lt;Foo&gt; b = expectedAnswer; Collection&lt;Matcher&lt;Foo&gt;&gt; bBeanMatchers = new LinkedList&lt;Matcher&lt;Foo&gt;&gt;(); // create a matcher that checks for the property values of each Foo for(Foo foo: B) bBeanMatchers.add(new SamePropertyValuesAs(foo)); assertThat(a, both(hasItems(bBeanMatchers)).and(hasSize(b.size()))) } } </code></pre> <p>The first test just uses the equalTo() matcher on every object (which will delegate to your equals implementation). If that's not strong enough, you can use the second case, which will use getters and setters to compare every element. Finally, you can even write your own matchers. The Hamcrest package doesn't come with a matcher for matching by field (as opposed to matching bean properties), but it's trivial to write a FieldMatcher (and indeed is a good exercise).</p> <p>The Matchers are a bit odd at first, but if you follow their example of making new Matchers have a static method that returns the matcher you can do a bunch of <code>import static</code>s and your code basically reads like an English sentence ("assert that a both has the items in b and has the same size as b"). You can build up a pretty impressive DSL with these things and make your test code a lot more elegant.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload