代码之家  ›  专栏  ›  技术社区  ›  monksy

找不到AssertArrayEquals(对象[]O1,对象[]O2)

  •  2
  • monksy  · 技术社区  · 15 年前

    我的设置:

      Netbeans 6.7
      Java6
      JUnit 4.5 added as the Test libraries
    

    当我尝试传递两个类数组(cast as object[])时,我得到错误“cannot find symbol”,并且我的测试用例将不会编译。

    我对其他断言语句没有问题,正如我所说,我使用的是JUnit4.5库。

    有没有人知道如何解决这个问题,或者观察到这种奇怪的行为?

    NetBeans可以通过其自动完成功能找到此函数声明,但无法找到它所在的位置,或者无法导航到源代码。

    样例代码:

    CustomObject[] coa = { new CustomObject() ....}
    CustomObject[] expected = { new CustomObject() ... } 
    assertArrayEquals((Object[])coa, (Object[])expected);
    
    4 回复  |  直到 11 年前
        1
  •  3
  •   Jon Skeet    15 年前

    好吧,assert.assertarrayequals是一个静态方法,正如您从正在工作的代码中看到的那样:

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

    但在您提供的代码中,您试图将其用作实例方法:

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

    只有当你静态地导入 Assert.* Assert.assertArrayEquals .

    现在,如果你的其他断言有效,我的 猜测 你仍然是从 TestCase (也就是说,编写JUnit测试的“老”方法)以及您的断言正在调用 TestCase.assertEquals 等。

    如果你能给出一个简短的但是 完成 一个断言有效但 assertArrayEquals 不,我们可能会搞清楚到底发生了什么。

        2
  •  1
  •   SingleShot    15 年前

    您不需要完全限定断言或将数组强制转换为对象数组。只需导入junit的适当部分并直接传入数组。不过,您应该颠倒示例中的参数顺序——您期望的是第一个(“Expecteds”),您实际从测试中得到的是第二个(“Actuals”)。这很好用:

    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 {}
    }
    
        3
  •  0
  •   monksy    15 年前

    问题是编译器拒绝查看实际的类。但它会有一条很糟糕的道路: org.junit.assert.assertArrayEquals(….

    我可以补充一句,这很烦人。

        4
  •  0
  •   Rhyous    11 年前

    我喜欢单张照片的答案,除了他的两个数组实际上包含相同的对象。如果对象不是同一个实际对象(不同对象的值相同,但应相等),会怎么样?

    所以我想我会加强他的回答来展示如何做到这一点。

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