Java junit如何断言我的方法返回一个特定的数组

Java junit如何断言我的方法返回一个特定的数组,java,junit,bluej,Java,Junit,Bluej,我有一个方法返回数组瞄准[]。在我的单元测试中,索引[0]处应该有一个元素(element1) 我的问题是,我如何构建我的声明来反映这一点?我需要断言getMostOfSpecies方法返回的数组在第一个索引值处包含element1 我的(不及格)测试是这样的 @Test public void getMostOfSpeciesTest() { try { birdList1.remember(sighting5); birdList1.remember(

我有一个方法返回数组瞄准[]。在我的单元测试中,索引[0]处应该有一个元素(element1)

我的问题是,我如何构建我的声明来反映这一点?我需要断言getMostOfSpecies方法返回的数组在第一个索引值处包含element1

我的(不及格)测试是这样的

@Test
public void getMostOfSpeciesTest()
{
    try {
        birdList1.remember(sighting5);
        birdList1.remember(sighting6);
        birdList1.remember(sighting7);
        birdList1.remember(sighting8);
        assertEquals(birdList1.getMostOfSpecies("SOBI"), Sighting[???what to put here???]);
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
}

您是否尝试过数组.equals()[1]?确保您也重写了Sighting类的equals()-方法

assertTrue( Arrays.equals(birdList1.getMostOfSpecies("SOBI"), yourSightingArray);
[1]

编辑:

如果需要检查数组是否包含一个特定对象,您可能会对
Arrays.binarySearch()
[2]或
Arrays.asList()
以及列表的contains()-方法感兴趣。所以您的断言语句应该如下所示

assertTrue( Arrays.binarySearch(birdList1.getMostOfSpecies("SOBI"), theElementYouExpect) >= 0);

[2]

andd,我的方法返回一个数组。我需要测试它是否包含一个特定的元素。对不起,你问题的标题是误导性的“返回一个特定的数组”。。。但是你可以在你的情况下使用Arrays.binarySearch,我想这就是你想要的。我将编辑我的答案。