ArrayList JUnit equality check

I want to use assertArrayEquals(ArrayList<Token>, ArrayList<Token>) with these arguments (i.e. arrayList from tokens). But Java tells me that I need to create such a method. Is there a way to check the equality of two lists of arrays of any type in Junit?

+15
source share
6 answers

I want to use assertArrayEquals(ArrayList<Token>, ArrayList<Token>) with these arguments (i.e. arrayList from tokens). But Java tells me that I need to create such a method.

He tells you that you need to create a method because there is no such method in the JUnit library. There is no such method in the JUnit library, because assertArrayEquals is for comparing arrays, and ArrayList not an array - it's a List .

Is there a way to check the equality of two lists of arrays of any type in Junit?

You can verify the equality of two ArrayLists (indeed, any two List objects) using equals , so you should be able to use the JUnit assertEquals method, and it will work fine.

+24
source

What you probably want to use is void org.junit.Assert.assertArrayEquals(Object[] expecteds, Object[] actuals) . You just need to convert the List to an array using the toArray() method, for example:

 ArrayList<Token> list1 = buildListOne(); // retrieve or build list ArrayList<Token> list2 = buildListTwo(); // retrieve or build other list with same items assertArrayEquals(list1.toArray(), list2.toArray()); 

Remember to import this statement.

 import static org.junit.Assert.assertArrayEquals; 

But these methods only work if the items in both lists are in the same order. If the order is not guaranteed, you need to sort the lists using the Collections.sort() method, but your object needs to implement the java.util.Comparable interface using a single int compareTo(T o) method.

PS: Another possible solution is to use assertEquals and transfer your list to Set, for example:

 assertEquals(new HashSet<Token>(list1), new HashSet<Token>(list2)); 
+5
source

Imagine myArraylist list of arrays with the contents of "one", "two", "three"

This works great:

  List<String> categories = asList("one", "two", "three"); assertTrue(myArraylist.equals(categories)); 

Do not forget to import: import static java.util.Arrays.asList;

+4
source

to try

 Assert.assertEquals(Object expected, Object actual); 

it works great with collections

+2
source

If you use some kind of junit framework on the shelf, such as unitils, etc., they have methods like assertReflectionEquals (similar to another framework), where you can use any two objects that use reflection. If you are not using any third-party junit structure, you can write your own similar generic method.

+1
source

You can override the equal method or hash code of an element type, for example: ArralyList, ArrayList - (either a primitive data type or a user data element), you can override the method and compare all your data inside this method and return true / a lie accordingly.

Then you can directly use assertEquals (arraylist1, arraylist2) for the junit test.

The following is an example of an object class

 public class Customer { String name; int age; @Override public boolean equals(Object obj) { if(this == obj) return true; // it checks if the argument is of the type Customer by comparing the classes // of the passed argument and this object. // if(!(obj instanceof Customer)) return false; ---> avoid. if(obj == null || obj.getClass()!= this.getClass()) return false; // type casting of the argument. Customer customer = (Customer) obj; if(customer.getName().equals(this.name) && (customer.getAge() == this.age)) return true; return false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 

}

The following is an example class for the Junit test:

 public class CustomerTest { @Test public void testCustomerMatch() { ArrayList<Customer> expectedCustomerListOutput; // add your data in this list ArrayList<Customer> actualCustomerListOutput;// add your data in this list //used the overridden equal method of trade objects assertEquals(expectedTradeOutput, actualTradeMatchList); } 

}

0
source

All Articles