2

I have method which returns ArrayList of object as below -

public <Range> getAllRanges(List<Range> ranges){
  //Some business logic
  ....
  List<Range> resultRanges = new ArrayList<>();
  .....
  return resultRanges;
}

I am writing Junit for this as -

@Test
public void someInputTest() {
    final List<Range> ranges = Arrays.asList(new Range(1, 3), new Range(2, 4));
    final List<Range> actual = myCalss.getAllRanges(ranges);
    final List<Range> expected = Arrays.asList(new Range(1, 4));
    assertEquals(expected, actual);
}

but I am getting Assertion Error like -

java.lang.AssertionError: expected: java.util.Arrays$ArrayList<[(1, 4)]> but was: java.util.ArrayList<[(1, 4)]>
....

After that I tried below code in test -

@Test
public void someInputTest() {
    final List<Range> ranges = Arrays.asList(new Range(1, 3), new Range(2, 4));
    final List<Range> actual = myCalss.getAllRanges(ranges);
    final List<Range> expected = new ArrayList<>();
    expected.add(new Range(1, 4));
    assertEquals(expected, actual);
}

but after that I am getting -

java.lang.AssertionError: expected: java.util.ArrayList<[(1, 4)]> but was: java.util.ArrayList<[(1, 4)]>
...

Now expected and actual are bot same but still assertion error. What I am doing wrong here.

4
  • This is basic Java. You are comparing two different Objects. Of course they are not equal... Add dependency for Hamcrest library, and add assertion like: assertThat(list1, containsInAnyOrder(list2.toArray())); Commented Mar 3, 2019 at 20:34
  • 5
    In Range have you implemented an equals method? Commented Mar 3, 2019 at 20:35
  • 1
    Two lists are equal if their corresponding elements are equal (and have the same length), so assertEquals is perfectly fine for testing two lists for equality. Make sure you have (correctly) implemented the equals method for Range class. E.g make sure this assertion passes: assertEquals(new Range(1, 4), new Range(1, 4)) Commented Mar 3, 2019 at 21:17
  • Thank you. I have override equals method and it is working. Commented Mar 4, 2019 at 18:29

4 Answers 4

5

Weirdly enough, Arrays.asList hasn't overridden equals, and thus even if it contains the same elements as another list of type ArrayList, they will not be compared as equal.

One solution is to wrap it into an ArrayList:

assertEquals(new ArrayList<>(Arrays.asList(...)), myArrayList);
Sign up to request clarification or add additional context in comments.

Comments

2

I had the same problem.You should add in your class , which used in list, equals and hashcode . After that it will work.

Comments

0

Adding Equals and Hashcode methods in the object which is being used in Arraylist will surely help in the case of assertEquals for arraylists of user defined objects.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

A quick and easy way: just simply add @EqualsAndHashCode on your Range class. @EqualsAndHashCode is provided by Project Lombok and generates hashCode and equals implementations from the fields of your object.

@EqualsAndHashCode
public class Range {
   private int x;
   // ...
}

And the following assertion works well in JUnit 4.13.1

Assert.assertEquals(expectedArrayList, actualArrayList);

https://projectlombok.org/features/EqualsAndHashCode

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.