Djordje Sevic
Djordje Sevic

Reputation: 21

Checking equality of ArrayList's content in Java JUnit

i have a problem with checking equality of two arraylists in JUnit tests. When i test equality of two lists, it only checks if their string representation is the same. It works for simple examples, like [1,2,3],[1,2,3] or when list contains objects that are string-represented with all of their properties. But when i have two lists that have same string representation but some objects have different properties how do i check their equality?

This is the example:

If i have Object of Class Human(int height, int weight, boolean alive), and toString() method is:

   public static String toString() {
        return this.height + "-" + this.weight;
   }

And i have two lists [20-30] and [20-30] but the object in first have

 boolean alive = false 

and in second

 boolean alive = true

how to tell the compiler that lists are not equal? Sorry for confusing explanation and thank you in advance!!! :D

Upvotes: 1

Views: 1909

Answers (4)

GhostCat
GhostCat

Reputation: 140407

The (imho) most readable way to compare lists:

assertThat(actualitems, is(expectedItems));

using assertThat() and the hamcrest is() matcher (see here for further reading).

And in order to make that work: you have to implement equals() (and as consequence hashCode() on your class (see here for how to do that).

In other words: if you want that such fields take part when comparing two objects, than you need to express that by making that "field by field" comparison part of an @Override equals() implementation. Any decent IDE can generate those methods for you - but when learning Java, it is a good exercise to do it yourself a few times.

Upvotes: 2

Lothar
Lothar

Reputation: 5449

A simple way would be

assertTrue("check equality", Arrays.equals(list1.toArray(), list2.toArray());

Only disadvantage is that you only get the information that they aren't equal but not where in the array the inequality happens.

Upvotes: 0

Abhishek Honey
Abhishek Honey

Reputation: 645

You need to override the hashcode and equals method. Here is the code

The output is

true false

public class test {
    public static void main(String[] args) {
        Human rob = new Human(110, 100, false);
        Human bob = new Human(110, 100, true);
        Human tob = new Human(110, 100, false);
        System.out.println(rob.equals(tob));
        System.out.println(rob.equals(bob));
    }
}

class Human {
    int height;
    int weight;
    boolean alive;

    public Human(int height, int weight, boolean alive) {
        super();
        this.height = height;
        this.weight = weight;
        this.alive = alive;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (alive ? 1231 : 1237);
        result = prime * result + height;
        result = prime * result + weight;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Human other = (Human) obj;
        if (alive != other.alive)
            return false;
        if (height != other.height)
            return false;
        if (weight != other.weight)
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Human [height=" + height + ", weight=" + weight + "]";
    }
}

Upvotes: 1

tomas
tomas

Reputation: 840

You can use Assert.class

 assertArrayEquals(Object[] expecteds, Object[] actuals) 

See http://junit.org/junit4/javadoc/4.8/org/junit/Assert.html

The equals-Methode of your Object have to compare all necessary attributes.

Upvotes: 2

Related Questions