# How to assert that two Lists are equal with TestNG?

TestNG supports a lot of assertions. It has the org.testng.Assert class, which extends the Java object class java.lang.object.

To compare two lists specifically, TestNG's Assert class has a method known as assertEquals(Object actual, Object expected) and there is an extended version of this method with customized message as assertEquals(Object actual, Object expected, String message).

This method returns True if −

• both the objects are Lists,

• both the lists are of same size, and

• if the elements of the lists are in the same order.

If any of these conditions are not True, it will return False.

In this article, we will discuss how to compare two lists in TestNG.

Approach/Algorithm to solve this problem −

• Step 1 − Create a TestNG class "NewTestngClass".

• Step 2 − Write three different @Test methods in the class, as shown in programming code section.

• 1st @Test Method − It has exactly 2 identical lists; satisfies all the 3 conditions and compares these 2 lists. This test will be passed.

• 2nd @Test Method − The size of 2 lists is not same and this test will throw the exception.

• 3rd @Test Method − The order of elements in the lists are not same and this test will also throw the exception.

• Step 3 − Now create the testNG.xml as given below to run the TestNG classes.

• Step 4 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.

## Example

Use the following code for the common TestNG class "NewTestngClass" −

src/ NewTestngClass.java

import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
public class NewTestngClass {
@Test
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
List<String> expectedName = new ArrayList<String>();
List<String> actualName = new ArrayList<String>();
Assert.assertEquals(actualName,expectedName, "Comaparsion of 2 lists");
}
@Test
public void testCase2() {
System.out.println("in test case 2 of NewTestngClass");
List<String> expectedName = new ArrayList<String>();
List<String> actualName = new ArrayList<String>();
Assert.assertEquals(actualName,expectedName,"Comparison of 2 lists");
}
@Test
public void testCase3() {
System.out.println("in test case 3 of NewTestngClass");
List<String> expectedName = new ArrayList<String>();
List<String> actualName = new ArrayList<String>();
}
}

testNG.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<classes>
<class name = "NewTestngClass"/>
</classes>
</test>
</suite>

## Output

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass

java.lang.AssertionError: Comaparsion of 2 lists: lists don't have the same size expected [3] but found [2]

Expected :3
Actual :2

in test case 3 of NewTestngClass

java.lang.AssertionError: Comaparsion of 2 lists: Lists differ at element [0]: ramesh != suresh expected [ramesh] but found [suresh]
Expected :ramesh
Actual :suresh

===============================================
Suite1
Total tests run: 3, Passes: 1, Failures: 2, Skips: 0
===============================================