开发者

HashSet in unit tests

I have a bunch of methods returning HashSet. I would like my unit test to check the state of these objects i.e confirm that someObject.getName() == "foobar".

However the hashset iterator order is not guaranteed so my unit tests fail some times. How do I write unit tests for something like this?

Eg:

@Test
    public void testRowsToBeRead(){
        HashSet<SomeObject> rows = new SomeObject().read();
        assertEquals(19, rows.size());

        for(SomeObject r:rows){
            //How do I confirm contents?
        }
    }

I think I might have accepted an answer too soon.

The problem I now have is that I have implemented the equals method, which checks only for 2 fields in the object per design (it is mimicking a DB table). However in my unit test, I want to check for all fields like description etc which is not in my equals. So if 2 of fields get swapped and these fields are not in my equ开发者_如何转开发als implementation, then the unit test gives a false positive.


My approach:

public void testRowsToBeRead(){
    HashSet<SomeObject> expectedRows = new HasSet<SomeObject();
    expectedRows.add(new SomeObject("abc"));
    expectedRows.add(new SomeObject("def"));

    HashSet<SomeObject> 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));
}

To rely on this test you may need other unit tests confirming that SomeObject's equals and hashCode methods work as they should be...

EDIT based on one of your comments

If you want to check for fields not part of the equals contract, you have to iterate through the set:

public void testRowsToBeRead(){
    HashSet<SomeObject> expectedRows = new HasSet<SomeObject();
    expectedRows.add(new SomeObject("a", "a1"));
    expectedRows.add(new SomeObject("b", "b1"));

    HashSet<SomeObject> 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()) && !expected.getField2().equals(r.getField2())) {
            // failed
        }
    }
}

In the above example SomeObject's equals may look like this, it only checks for field1:

@Override
public boolean equals(Object other) {
    return this.getField1().equals( ((SomeObject) other).getField1() );
}

No question, depending on your concrete use case this may become more complex. Hope this helps...


You can use LinkedHashSet which guarantee order.


Try to use assertThat:

Assert.assertThat(r.getName(), AnyOf.anyOf(Is.is("foobar1"), Is.is("foobar2"), ...));


You can use Assert.assertEquals() on pretty much any collection with TestNG and probably JUnit as well.

If you want to roll this yourself, remove all the elements of the actual set from the expected set and assert that the expected set's size is zero when you're done.


I recommend use HashSet.equals(Object).

JavaDoc says:

This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it returns false. If so, it returns containsAll((Collection) o).

So I think "home"'s "alternative 2" is enough.

plus: Although HashSet doesn't guarantee the order, equaled HashSets are in same order and size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜