From 6a2fcd54c33f583e28b6bd379948066a18171a05 Mon Sep 17 00:00:00 2001 From: Vishal Date: Wed, 12 Aug 2020 09:57:39 +0530 Subject: [PATCH 1/3] Add unit test to assert two list equality without considering order of elements in it. --- testing-modules/testing-assertions/pom.xml | 12 +++++ .../OrderAgnosticListComparison.java | 46 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml index 0a7c4b0860..fe8c86d058 100644 --- a/testing-modules/testing-assertions/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -24,5 +24,17 @@ 3.15.0 test + + org.hamcrest + hamcrest-all + 1.3 + test + + + org.apache.commons + commons-collections4 + 4.4 + test + diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java new file mode 100644 index 0000000000..b03723e5bd --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java @@ -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 first = Arrays.asList(1, 3, 4, 6, 8); + private final List second = Arrays.asList(8, 1, 6, 3, 4); + private final List 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)); + } +} From 3cbfd383edd32b2848286c643fbc53b5f7609718 Mon Sep 17 00:00:00 2001 From: Vishal Date: Mon, 17 Aug 2020 18:06:20 +0530 Subject: [PATCH 2/3] Refactor | Add common formatting, remove comments, update test class name,follow test method naming convention and use Junit5 --- testing-modules/testing-assertions/pom.xml | 22 +++++++++ .../OrderAgnosticListComparison.java | 46 ------------------- .../OrderAgnosticListComparisonUnitTest.java | 40 ++++++++++++++++ 3 files changed, 62 insertions(+), 46 deletions(-) delete mode 100644 testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java create mode 100644 testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml index fe8c86d058..5bb6f5faaa 100644 --- a/testing-modules/testing-assertions/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -18,6 +18,18 @@ logback-classic 1.2.3 + + org.junit.jupiter + junit-jupiter-engine + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.6.2 + test + org.assertj assertj-core @@ -37,4 +49,14 @@ test + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java deleted file mode 100644 index b03723e5bd..0000000000 --- a/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparison.java +++ /dev/null @@ -1,46 +0,0 @@ -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 first = Arrays.asList(1, 3, 4, 6, 8); - private final List second = Arrays.asList(8, 1, 6, 3, 4); - private final List 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)); - } -} diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java new file mode 100644 index 0000000000..9ef6f203af --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.listassert; + +import org.apache.commons.collections4.CollectionUtils; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class OrderAgnosticListComparisonUnitTest { + + private final List first = Arrays.asList(1, 3, 4, 6, 8); + private final List second = Arrays.asList(8, 1, 6, 3, 4); + private final List 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() { + assertThat(first, Matchers.containsInAnyOrder(second.toArray())); + } + + @Test + public void whenTestingForOrderAgnosticEquality_ShouldBeTrueIfEqualOtherwiseFalse() { + assertTrue(CollectionUtils.isEqualCollection(first, second)); + assertFalse(CollectionUtils.isEqualCollection(first, third)); + } +} From aa223e69eada94f4771d69cfecce0771a6742b52 Mon Sep 17 00:00:00 2001 From: Vishal Date: Sun, 23 Aug 2020 10:35:43 +0530 Subject: [PATCH 3/3] Add tests for AssertJ example and update assertj-core dependency version. --- testing-modules/testing-assertions/pom.xml | 2 +- .../OrderAgnosticListComparisonUnitTest.java | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml index 5bb6f5faaa..fa0f666c7f 100644 --- a/testing-modules/testing-assertions/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -33,7 +33,7 @@ org.assertj assertj-core - 3.15.0 + 3.16.1 test diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java index 9ef6f203af..bf278cea90 100644 --- a/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/listassert/OrderAgnosticListComparisonUnitTest.java @@ -1,13 +1,14 @@ 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.hamcrest.MatcherAssert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -29,7 +30,7 @@ public class OrderAgnosticListComparisonUnitTest { @Test public void whenTestingForOrderAgnosticEquality_ShouldBeEqual() { - assertThat(first, Matchers.containsInAnyOrder(second.toArray())); + MatcherAssert.assertThat(first, Matchers.containsInAnyOrder(second.toArray())); } @Test @@ -37,4 +38,17 @@ public class OrderAgnosticListComparisonUnitTest { assertTrue(CollectionUtils.isEqualCollection(first, second)); assertFalse(CollectionUtils.isEqualCollection(first, third)); } + + @Test + void whenTestingForOrderAgnosticEqualityBothList_ShouldBeEqual() { + assertThat(first).hasSameElementsAs(second); + } + + @Test + void whenTestingForOrderAgnosticEqualityBothList_ShouldNotBeEqual() { + List a = Arrays.asList("a", "a", "b", "c"); + List b = Arrays.asList("a", "b", "c"); + + assertThat(a).hasSameElementsAs(b); + } }