26

assertArrayEquals(ArrayList<Token>, ArrayList<Token>)これらの引数 (つまり、トークンの arrayList)を使用したいと考えています。しかし、Java は、そのようなメソッドを作成する必要があることを教えてくれます。Junit でどのようなタイプの 2 つの arrayLists が等しいかをテストする方法はありますか?

4

6 に答える 6

32

assertArrayEquals(ArrayList<Token>, ArrayList<Token>)これらの引数 (つまり、トークンの arrayList)を使用したいと考えています。しかし、Java は、そのようなメソッドを作成する必要があることを教えてくれます。

JUnit ライブラリにそのようなメソッドがないため、メソッドを作成する必要があることを示しています。JUnit ライブラリにはそのようなメソッドはありません。 はarraysassertArrayEqualsを比較するためのものであり、 andは配列ではなく、.ArrayListList

Junit でどのようなタイプの 2 つの arrayLists が等しいかをテストする方法はありますか?

ArrayListsを使用して 2 つ(実際には任意の 2 つのListオブジェクト)が等しいことを確認equalsできるため、JUnit のメソッドを使用できるはずであり、問​​題assertEqualsなく動作します。

于 2013-03-17T02:59:02.010 に答える
11

おそらく使用したいのはvoid org.junit.Assert.assertArrayEquals(Object[] expecteds, Object[] actuals). toArray()次のように、メソッドを使用して List を配列に変換するだけです。

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

このアサートをインポートすることを忘れないでください。

import static org.junit.Assert.assertArrayEquals;

ただし、この方法は、両方のリストの項目が同じ順序である場合にのみ機能します。順序が保証されていない場合は、メソッドでリストを並べ替える必要がありますが、オブジェクトは 1 つのメソッドでインターフェイスCollections.sort()を実装する必要があります。java.util.Comparableint compareTo(T o)

PS: 他の可能な解決策は、 assertEquals を使用して、次のようにリストを Set にラップすることです。

assertEquals(new HashSet<Token>(list1), new HashSet<Token>(list2));
于 2013-09-18T12:41:58.020 に答える
6

myArraylist内容を含む配列リストを想像してください"one", "two", "three"

これはうまくいきます:

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

インポートすることを忘れないでください:import static java.util.Arrays.asList;

于 2015-11-18T19:42:59.723 に答える
4

試す

Assert.assertEquals(Object expected, Object actual);

コレクションで問題なく動作します

于 2013-03-17T04:15:21.120 に答える
2

Element タイプの「equals」または「hashcode」メソッドをオーバーライドできます。例: ArralyList、ArrayList - (プリミティブ データ型またはカスタム データ要素のいずれか)、メソッドをオーバーライドし、そのメソッド内のすべてのデータを比較して true/ を返すことができます。したがって、false。

その後、junit テストに assertEquals(arraylist1 , arraylist2) を直接使用できます。

以下はオブジェクトクラスのサンプルです

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

}

以下は、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);
}

}

于 2019-08-15T16:54:17.710 に答える
1

unitils などの既製の junit フレームワークを使用している場合、それらには assertReflectionEquals (他のフレームワークと同様) のようなメソッドがあり、そこで任意の 2 つのオブジェクトをリフレクションで使用できます。サードパーティのjunitフレームワークを使用していない場合は、独自の同様の汎用メソッドを作成できます

于 2013-03-17T02:22:47.737 に答える