Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 equals implementation, then the unit test gives a false positive.

like image 681
Kapsh Avatar asked Aug 12 '11 15:08

Kapsh


People also ask

How do you unit test a set?

Just put your expected values in a Set and then use assertEquals on the expected and actual set. That works a charm, e.g. Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set.

Does HashSet use equals or compareTo?

Comparison methodHashSet uses equal() and hashcode() methods to compare the elements, while TreeSet we can implements compareTo() method of Comparator interface so we have compare() and compareTo() method ,TreeSet does not use equal() and hashcode() method.

Why HashSet has no get method?

HashSet can not guarantee insertion order so no point in get method. What are you missing is implementing equals and use contains() which will iterate and find the object.


1 Answers

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...

like image 139
home Avatar answered Sep 19 '22 07:09

home