Skip to content

Commit

Permalink
Comparing collection using equals method for assertEquals (#2460)
Browse files Browse the repository at this point in the history
* Comparing collection with equals method.

* replaced all occurence of comparison operator with equals methods for non primitive data types

* added test cases for collections, sets and list

* build failing

Co-authored-by: Prashant Maroti <prashant@ip-192-168-1-2.ap-southeast-1.compute.internal>
Co-authored-by: Prashant Maroti <prashant@ip-192-168-1-4.ap-southeast-1.compute.internal>
  • Loading branch information
3 people authored Jan 26, 2021
1 parent 378e63e commit 841ae94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2296: Fix for assertEquals not working for sets as order is not guaranteed. (Prashant Maroti)
Fixed: GITHUB-2465: Fix bux where Strings.join returns empty String
Fixed: GITHUB-1632: throwing SkipException sets iTestResult status to Failure instead of Skip (Julien Herr & Krishnan Mahadevan)
New : GITHUB-2456: Add onDataProviderFailure listener (Krishnan Mahadevan)
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/testng/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import static org.testng.internal.EclipseInterface.ASSERT_MIDDLE;
import static org.testng.internal.EclipseInterface.ASSERT_RIGHT;
import static org.testng.internal.EclipseInterface.ASSERT_UNEQUAL_LEFT;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.testng.collections.Lists;

Expand Down Expand Up @@ -165,7 +165,7 @@ private static boolean areEqualImpl(Object actual, Object expected) {
* returns not equal reason or null if equal
**/
private static String getArrayNotEqualReason(Object actual, Object expected) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return null;
}
if (null == expected) {
Expand Down Expand Up @@ -975,7 +975,7 @@ public static void assertNull(Object object, String message) {
* @param message the assertion error message
*/
public static void assertSame(Object actual, Object expected, String message) {
if (expected == actual) {
if (Objects.equals(actual, expected)) {
return;
}
failNotSame(actual, expected, message);
Expand All @@ -1001,7 +1001,7 @@ public static void assertSame(Object actual, Object expected) {
* @param message the assertion error message
*/
public static void assertNotSame(Object actual, Object expected, String message) {
if (expected == actual) {
if (Objects.equals(actual, expected)) {
failSame(actual, expected, message);
}
}
Expand Down Expand Up @@ -1074,7 +1074,7 @@ public static void assertEquals(Collection<?> actual, Collection<?> expected) {
* @param message the assertion error message
*/
public static void assertEquals(Collection<?> actual, Collection<?> expected, String message) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return;
}

Expand Down Expand Up @@ -1126,7 +1126,7 @@ public static void assertEquals(Iterator<?> actual, Iterator<?> expected) {
* @param message the assertion error message
*/
public static void assertEquals(Iterator<?> actual, Iterator<?> expected, String message) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return;
}
if (actual == null || expected == null) {
Expand Down Expand Up @@ -1183,7 +1183,7 @@ public static void assertEquals(Iterable<?> actual, Iterable<?> expected) {
* @param message the assertion error message
*/
public static void assertEquals(Iterable<?> actual, Iterable<?> expected, String message) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return;
}

Expand All @@ -1210,7 +1210,7 @@ public static void assertEquals(Iterable<?> actual, Iterable<?> expected, String
* @param message the assertion error message
*/
public static void assertEquals(Object[] actual, Object[] expected, String message) {
if (actual == expected) {
if (Arrays.equals(actual, expected)) {
return;
}

Expand Down Expand Up @@ -1256,7 +1256,7 @@ public static void assertEquals(Object[] actual, Object[] expected, String messa
* @param message the assertion error message
*/
public static void assertEqualsNoOrder(Object[] actual, Object[] expected, String message) {
if (actual == expected) {
if (Arrays.equals(actual, expected)) {
return;
}

Expand Down Expand Up @@ -1327,7 +1327,7 @@ public static void assertEquals(Set<?> actual, Set<?> expected) {
* returns not equal reason or null if equal
**/
private static String getNotEqualReason(Set<?> actual, Set<?> expected) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return null;
}

Expand All @@ -1336,7 +1336,7 @@ private static String getNotEqualReason(Set<?> actual, Set<?> expected) {
return "Sets not equal: expected: " + expected + " and actual: " + actual;
}

if (!actual.equals(expected)) {
if (!Objects.equals(actual, expected)) {
return "Sets differ: expected " + expected + " but got " + actual;
}
return null;
Expand Down Expand Up @@ -1365,7 +1365,7 @@ public static void assertEquals(Set<?> actual, Set<?> expected, String message)
* returns not equal deep reason or null if equal
**/
private static String getNotEqualDeepReason(Set<?> actual, Set<?> expected) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return null;
}

Expand Down Expand Up @@ -1413,7 +1413,7 @@ public static void assertEquals(Map<?, ?> actual, Map<?, ?> expected) {
}

private static String getNotEqualReason(Map<?, ?> actual, Map<?, ?> expected) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return null;
}

Expand Down Expand Up @@ -1470,7 +1470,7 @@ public static void assertEqualsDeep(Map<?, ?> actual, Map<?, ?> expected) {
* returns not equal deep reason or null if equal
**/
private static String getNotEqualDeepReason(Map<?, ?> actual, Map<?, ?> expected) {
if (actual == expected) {
if (Objects.equals(actual, expected)) {
return null;
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/testng/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.collections.Sets;
import testhelper.PerformanceUtils;
Expand Down Expand Up @@ -41,6 +42,27 @@ public void nullSetAssertEquals() {
Assert.assertEquals(actual, expected);
}

@Test
public void testCollectionAssertEquals() {
final Collection<Asymmetric> expected = Sets.newHashSet(new Asymmetric(10, 'a'), new Asymmetric(11, 'b'));
final Collection<Asymmetric> actual = Sets.newHashSet(new Asymmetric(11, 'b'), new Asymmetric(10, 'a'));
Assert.assertEquals(actual, expected);
}

@Test
public void testListAssertNotEquals() {
final Collection<Asymmetric> expected = Lists.newArrayList(new Asymmetric(10, 'a'), new Asymmetric(11, 'b'));
final Collection<Asymmetric> actual = Lists.newArrayList(new Asymmetric(11, 'b'), new Asymmetric(10, 'a'));
Assert.assertNotEquals(actual, expected);
}

@Test
public void testSetAssertEquals() {
final Set<Asymmetric> expected = Sets.newHashSet(new Asymmetric(10, 'a'), new Asymmetric(11, 'b'));
final Set<Asymmetric> actual = Sets.newHashSet(new Asymmetric(11, 'b'), new Asymmetric(10, 'a'));
Assert.assertEquals(actual, expected);
}

@Test
public void nullMapAssertEquals() {
Map expected = null;
Expand Down

0 comments on commit 841ae94

Please sign in to comment.