ArrayList equality JUnit testing

2020-07-02 05:10发布

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

标签: java junit
6条回答
倾城 Initia
2楼-- · 2020-07-02 05:48

try

Assert.assertEquals(Object expected, Object actual);

it works fine with Collections

查看更多
3楼-- · 2020-07-02 05:49

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

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

Is there a way to test for the equality of two arrayLists of whatever type in Junit?

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

查看更多
我命由我不由天
4楼-- · 2020-07-02 05:58

You can override "equals" or "hashcode" method of Element type eg : ArralyList, ArrayList - (either primitive data type or custom data element), You can override the method, and compare all you data inside in that method and return true/ false accordingly.

You can then directly use assertEquals(arraylist1 , arraylist2) for your junit test.

Below is sample 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;
}

}

Below is the sample class for 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);
}

}

查看更多
手持菜刀,她持情操
5楼-- · 2020-07-02 06:03

What you probably want to use is void org.junit.Assert.assertArrayEquals(Object[] expecteds, Object[] actuals). You just need to convert List to array with toArray() method, like that:

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());

Don't forget to import this assert.

import static org.junit.Assert.assertArrayEquals;

But this methods only works if items in both lists have same order. If the order is not guaranteed, then you need to sort the lists with Collections.sort() method, but your object need to implement java.util.Comparable interface with one method int compareTo(T o).

PS: The other possible solution is to use assertEquals and wrap your list into Set, like that:

assertEquals(new HashSet<Token>(list1), new HashSet<Token>(list2));
查看更多
够拽才男人
6楼-- · 2020-07-02 06:10

Imagine myArraylist is an array list with contents "one", "two", "three"

This works fine:

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

Dont forget to import: import static java.util.Arrays.asList;

查看更多
Summer. ? 凉城
7楼-- · 2020-07-02 06:11

If you are using some off the shelf junit framework like unitils etc they have methods like assertReflectionEquals (similary other framework does) where you can any two objects using reflection . If you are not using any third party junit framework , you can write your own similar generic method

查看更多
登录 后发表回答