Add unit test to assert two list equality without considering order of elements in it.

This commit is contained in:
Vishal 2020-08-12 09:57:39 +05:30
parent 0423536cf4
commit 6a2fcd54c3
2 changed files with 58 additions and 0 deletions

View File

@ -24,5 +24,17 @@
<version>3.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,46 @@
package com.baeldung.listassert;
import org.hamcrest.Matchers;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class OrderAgnosticListComparison {
private final List<Integer> first = Arrays.asList(1, 3, 4, 6, 8);
private final List<Integer> second = Arrays.asList(8, 1, 6, 3, 4);
private final List<Integer> third = Arrays.asList(1, 3, 3, 6, 6);
//In this test using simple JUnit assertion
@Test
public void whenTestingForOrderAgnosticEqualityShouldBeTrue() {
assertTrue(first.size() == second.size() &&
first.containsAll(second) && second.containsAll(first));
}
@Test
public void whenTestingForOrderAgnosticEqualityShouldBeFalse() {
assertFalse(first.size() == third.size() &&
first.containsAll(third) && third.containsAll(first));
}
//In this test using Hamcrest lib apis for assertion
@Test
public void whenTestingForOrderAgnosticEqualityShouldBeEqual() {
assertThat(first, Matchers.containsInAnyOrder(second.toArray()));
}
//In this test asserting lists using Apache Commons apis
@Test
public void whenTestingForOrderAgnosticEqualityShouldBeTrueIfEqualOtherwiseFalse() {
assertTrue(CollectionUtils.isEqualCollection(first, second));
assertFalse(CollectionUtils.isEqualCollection(first, third));
}
}