Refactor | Add common formatting, remove comments, update test class name,follow test method naming convention and use Junit5

This commit is contained in:
Vishal 2020-08-17 18:06:20 +05:30
parent 6a2fcd54c3
commit 3cbfd383ed
3 changed files with 62 additions and 46 deletions

View File

@ -18,6 +18,18 @@
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</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>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
@ -37,4 +49,14 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -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<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));
}
}

View File

@ -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<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() {
assertThat(first, Matchers.containsInAnyOrder(second.toArray()));
}
@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeTrueIfEqualOtherwiseFalse() {
assertTrue(CollectionUtils.isEqualCollection(first, second));
assertFalse(CollectionUtils.isEqualCollection(first, third));
}
}