assertArrayEquals(ArrayList<Token>, ArrayList<Token>)
これらの引数 (つまり、トークンの arrayList)を使用したいと考えています。しかし、Java は、そのようなメソッドを作成する必要があることを教えてくれます。Junit でどのようなタイプの 2 つの arrayLists が等しいかをテストする方法はありますか?
6 に答える
assertArrayEquals(ArrayList<Token>, ArrayList<Token>)
これらの引数 (つまり、トークンの arrayList)を使用したいと考えています。しかし、Java は、そのようなメソッドを作成する必要があることを教えてくれます。
JUnit ライブラリにそのようなメソッドがないため、メソッドを作成する必要があることを示しています。JUnit ライブラリにはそのようなメソッドはありません。 はarraysassertArrayEquals
を比較するためのものであり、 andは配列ではなく、.ArrayList
List
Junit でどのようなタイプの 2 つの arrayLists が等しいかをテストする方法はありますか?
ArrayLists
を使用して 2 つ(実際には任意の 2 つのList
オブジェクト)が等しいことを確認equals
できるため、JUnit のメソッドを使用できるはずであり、問題assertEquals
なく動作します。
おそらく使用したいのは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.Comparable
int compareTo(T o)
PS: 他の可能な解決策は、 assertEquals を使用して、次のようにリストを Set にラップすることです。
assertEquals(new HashSet<Token>(list1), new HashSet<Token>(list2));
myArraylist
内容を含む配列リストを想像してください"one", "two", "three"
これはうまくいきます:
List<String> categories = asList("one", "two", "three");
assertTrue(myArraylist.equals(categories));
インポートすることを忘れないでください:import static java.util.Arrays.asList;
試す
Assert.assertEquals(Object expected, Object actual);
コレクションで問題なく動作します
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);
}
}
unitils などの既製の junit フレームワークを使用している場合、それらには assertReflectionEquals (他のフレームワークと同様) のようなメソッドがあり、そこで任意の 2 つのオブジェクトをリフレクションで使用できます。サードパーティのjunitフレームワークを使用していない場合は、独自の同様の汎用メソッドを作成できます