2009-10-12 43 views
2

我的設置:assertArrayEquals(Object []對象O1,對象[] O2)無法找到

Netbeans 6.7 
    Java6 
    JUnit 4.5 added as the Test libraries 

當我嘗試在二級陣列通過(強制轉換爲對象[])我得到錯誤「不能找到符號「,我的測試用例不會編譯。

我沒有與其他assert語句的問題,正如我所說我正在使用JUnit 4.5庫。

有沒有人有如何解決這個問題的線索,或觀察到這種古怪的行爲?

Netbeans能夠通過它的自動完成功能找到這個函數聲明,但它無法找到它所在的位置,或者可以導航到源。

示例代碼:

CustomObject[] coa = { new CustomObject() ....} 
CustomObject[] expected = { new CustomObject() ... } 
assertArrayEquals((Object[])coa, (Object[])expected); 
+0

請張貼測試代碼。 – SingleShot

回答

3

嘛,Assert.assertArrayEquals是一個靜態方法,你可以從你的代碼中看到它的工作:

org.junit.Assert.assertArrayEquals(....) 

但你給的代碼,你試圖用它作爲實例方法:

assertArrayEquals((Object[])coa, (Object[])expected); 

如果你靜態導入了Assert.*Assert.assertArrayEquals這隻會工作。現在

,如果你的另一說法是工作,我猜測是,你仍然TestCase獲得(即編寫JUnit測試的「老」的方式),以及您的斷言呼籲TestCase.assertEquals

如果你可以給出一個簡短但完整的單個測試的例子,其中一個斷言可以工作,但是assertArrayEquals沒有,我們可以計算出發生了什麼。

+0

所有assertArrayEquals均失敗。這個解釋最有意義。 – monksy

0

問題是,編譯器拒絕尋找到實際的類..但它會與abosulte路徑: org.junit.Assert.assertArrayEquals(... 。

有點討厭我可能會增加。

+1

這沒有任何意義。請發佈您的代碼。 – SingleShot

+0

這沒有任何意義。我沒有匹配的方法的範圍內assertArrayEquals(對象[],對象[])的任何過載的方法。定義存在的唯一地方是在org.junit.Assert中。 – monksy

1

你並不需要完全限定的斷言或投你的數組對象數組。只需導入的JUnit的適當部分,並在你的陣列直接通過。您應該扭轉參數順序來回在你的例子中 - 你期望的是第一個(「預期」),你實際從測試中得到的第二個(「實際」)。這工作得很好:

import org.junit.*; 
import static org.junit.Assert.*; 

public class TestJUnitActuallyWorks { 

    @Test 
    public void myArraysShouldBeIdentical() { 

     CustomObject one = new CustomObject(); 
     CustomObject two = new CustomObject(); 
     CustomObject three = new CustomObject(); 

     CustomObject[] expecteds = { one, two, three }; 
     CustomObject[] actuals = { one, two, three }; 
     assertArrayEquals(expecteds, actuals); 
    } 

    private static class CustomObject {} 
} 
+0

這是我在做什麼,並編譯只是似乎並不想指出在org.junit.Assert實施*。類。 – monksy

+0

第三次是魅力:請把你的測試代碼........ – SingleShot

0

我喜歡SingleShot的答案,除了他的兩個數組實際上包含相同的對象。如果對象不是相同的實際對象(不同的對象相同的值但應該相等)。

所以我想我會加強他的答案,展示如何做到這一點。

@Test 
public void myArraysShouldBeIdentical() { 

    CustomObject one1 = new CustomObject("one"); 
    CustomObject two1 = new CustomObject("two"); 
    CustomObject three1 = new CustomObject("three"); 

    CustomObject one2 = new CustomObject("one"); 
    CustomObject two2 = new CustomObject("two"); 
    CustomObject three2 = new CustomObject("three"); 

    CustomObject[] expecteds = { one1, two1, three1 }; 
    CustomObject[] actuals = { one2, two2, three2 }; 
    assertArrayEquals(expecteds, actuals); 
} 

private static class CustomObject { 
    public String value; 

    CustomObject(String inValue) 
    { 
     value = inValue; 
    } 

    @Override 
    public int hashCode() { 
     return value.hashCode(); 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) 
      return false; 
     if (obj == this) 
      return true; 
     if (!(obj instanceof CustomObject)) 
      return false; 

     CustomObject rhs = (CustomObject) obj; 
     return value == rhs.value; 
    } 
} 
相關問題