1

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

2
  • 1. Check each item in List, 2. Override equals method in Human class Commented Aug 31, 2017 at 11:06
  • 1
    Nice newbie question ... and nice to see that you quickly accepted an answer. Where you still should study this topic, instead just blindly copying some code somebody wrote for you. Commented Aug 31, 2017 at 11:27

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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 + "]";
    }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.