Merge pull request #9854 from vishal1023/master

AssertEquals 2 Lists ignore order
This commit is contained in:
Jonathan Cook 2020-08-30 21:48:51 +02:00 committed by GitHub
commit 50563c7f63
2 changed files with 89 additions and 1 deletions

View File

@ -18,11 +18,45 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>1.2.3</version> <version>1.2.3</version>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<version>3.15.0</version> <version>3.16.1</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> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -0,0 +1,54 @@
package com.baeldung.listassert;
import org.apache.commons.collections4.CollectionUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class OrderAgnosticListComparisonUnitTest {
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);
@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeTrue() {
assertTrue(first.size() == second.size() && first.containsAll(second) && second.containsAll(first));
}
@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeFalse() {
assertFalse(first.size() == third.size() && first.containsAll(third) && third.containsAll(first));
}
@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeEqual() {
MatcherAssert.assertThat(first, Matchers.containsInAnyOrder(second.toArray()));
}
@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeTrueIfEqualOtherwiseFalse() {
assertTrue(CollectionUtils.isEqualCollection(first, second));
assertFalse(CollectionUtils.isEqualCollection(first, third));
}
@Test
void whenTestingForOrderAgnosticEqualityBothList_ShouldBeEqual() {
assertThat(first).hasSameElementsAs(second);
}
@Test
void whenTestingForOrderAgnosticEqualityBothList_ShouldNotBeEqual() {
List<String> a = Arrays.asList("a", "a", "b", "c");
List<String> b = Arrays.asList("a", "b", "c");
assertThat(a).hasSameElementsAs(b);
}
}