Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My approach:</p> <pre><code>public void testRowsToBeRead(){ HashSet&lt;SomeObject&gt; expectedRows = new HasSet&lt;SomeObject(); expectedRows.add(new SomeObject("abc")); expectedRows.add(new SomeObject("def")); HashSet&lt;SomeObject&gt; rows = new SomeObject().read(); // alternative 1 assertEquals(19, rows.size()); for(SomeObject r:rows){ if (!expectedRows.contains(r)) { // test failed } } // alternative 2 assertTrue(expectedRows.equals(rows)); } </code></pre> <p>To rely on this test you may need other unit tests confirming that <code>SomeObject</code>'s <code>equals</code> and <code>hashCode</code> methods work as they should be...</p> <p><strong>EDIT based on one of your comments</strong></p> <p>If you want to check for fields not part of the <code>equals</code> contract, you have to iterate through the set:</p> <pre><code>public void testRowsToBeRead(){ HashSet&lt;SomeObject&gt; expectedRows = new HasSet&lt;SomeObject(); expectedRows.add(new SomeObject("a", "a1")); expectedRows.add(new SomeObject("b", "b1")); HashSet&lt;SomeObject&gt; rows = new SomeObject().read(); for(SomeObject r : rows) { SomeObject expected = expectedRows.get(r); // equals and hashCode must still match if (expected == null) { // failed } if (!expected.getField1().equals(r.getField1()) &amp;&amp; !expected.getField2().equals(r.getField2())) { // failed } } } </code></pre> <p>In the above example <code>SomeObject</code>'s <code>equals</code> may look like this, it only checks for <code>field1</code>:</p> <pre><code>@Override public boolean equals(Object other) { return this.getField1().equals( ((SomeObject) other).getField1() ); } </code></pre> <p>No question, depending on your concrete use case this may become more complex. Hope this helps...</p>
 

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