找不到assertArrayEquals(Object[] o1, Object[] o2)
我的设置:
Netbeans 6.7
Java6
JUnit 4.5 added as the Test libraries
当我尝试传递两个类数组(转换为 Object[])时,我收到错误“找不到符号”,并且我的测试用例将无法编译。
我对其他断言语句没有问题,正如我所说,我正在使用 JUnit 4.5 库。
有谁知道如何解决这个问题,或者观察到这种奇怪的行为?
Netbeans 能够通过其自动完成功能找到此函数声明,但无法找到它所在的位置,也无法导航到源。
示例代码:
CustomObject[] coa = { new CustomObject() ....}
CustomObject[] expected = { new CustomObject() ... }
assertArrayEquals((Object[])coa, (Object[])expected);
My setup:
Netbeans 6.7
Java6
JUnit 4.5 added as the Test libraries
When I try to pass in two class arrays (cast as Object[]) I get the error "cannot find symbol" and my test case will not compile.
I do not have an issue with the other assert statements, and as I said I am using the JUnit 4.5 libraries.
Does anyone have a clue on how to fix this, or observed this quirky behavior?
Netbeans is able to find this function declaration through its autocomplete, but it is unable to find where it is located at, or can navigate to the sources.
Sample code:
CustomObject[] coa = { new CustomObject() ....}
CustomObject[] expected = { new CustomObject() ... }
assertArrayEquals((Object[])coa, (Object[])expected);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,Assert.assertArrayEquals 是一个静态方法,正如您从正在运行的代码中看到的那样:
但是在您给出的代码中,您试图将它用作实例方法:
只有在静态导入时才有效
Assert.*
或Assert.assertArrayEquals
。现在,如果您的其他断言有效,我的猜测是您仍然从
TestCase
(即编写 JUnit 测试的“旧”方式)派生,并且您的断言正在调用TestCase.assertEquals
等。如果您可以给出一个简短但完整的单元测试示例,其中一个断言有效,但
assertArrayEquals
无效,我们也许可以弄清楚发生了什么。Well, Assert.assertArrayEquals is a static method, as you can see from your code which is working:
But in the code you were giving, you were trying to use it as an instance method:
That would only work if you'd statically imported
Assert.*
orAssert.assertArrayEquals
.Now, if your other assertions are working, my guess is that you're still deriving from
TestCase
(i.e. the "old" way of writing JUnit tests) and that your assertions are callingTestCase.assertEquals
etc.If you could give a short but complete example of a unit test where one assertion works but
assertArrayEquals
doesn't, we could probably work out what's going on.您不需要完全限定断言或将数组转换为对象数组。只需导入 JUnit 的适当部分并直接传入数组即可。不过,您应该颠倒示例中的参数顺序 - 您期望的内容排在第一位(“预期”),您从测试中实际获得的内容排在第二位(“实际值”)。这工作正常:
You don't need to fully qualify the assertion or cast your arrays to object arrays. Just import the proper parts of JUnit and pass in your arrays directly. You should reverse the parameter order from your example though - what you expect comes first ("expecteds"), what you actually get from the test comes second ("actuals"). This works fine:
问题是编译器拒绝查看实际的类..但它会使用 abosulte 路径:
org.junit.Assert.assertArrayEquals(....
我可能会补充一点,这很烦人。
The issue was that the compiler was refusing to look into the actual class.. but it would with an abosulte path:
org.junit.Assert.assertArrayEquals(....
Rather annoying I may add.
我喜欢 SingleShot 的答案,除了他的两个数组实际上包含相同的对象。如果这些对象不是相同的实际对象(不同的对象具有相同的值但应该相等)怎么办?
所以我想我应该增强他的答案以展示如何做到这一点。
I like SingleShot's answer except his two arrays actually contain the same objects. What if the objects aren't the same actual objects (different objects same values but should be equal).
So I thought I would enhance his answer to show how to do this.