找不到assertArrayEquals(Object[] o1, Object[] o2)

发布于 2024-08-07 15:54:04 字数 499 浏览 7 评论 0原文

我的设置:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你げ笑在眉眼 2024-08-14 15:54:04

好吧,Assert.assertArrayEquals 是一个静态方法,正如您从正在运行的代码中看到的那样:

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

但是在您给出的代码中,您试图将它用作实例方法:

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

只有在静态导入时才有效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:

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

But in the code you were giving, you were trying to use it as an instance method:

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

That would only work if you'd statically imported Assert.* or Assert.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 calling TestCase.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.

泪冰清 2024-08-14 15:54:04

您不需要完全限定断言或将数组转换为对象数组。只需导入 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 {}
}

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:

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 {}
}
蒲公英的约定 2024-08-14 15:54:04

问题是编译器拒绝查看实际的类..但它会使用 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.

她说她爱他 2024-08-14 15:54:04

我喜欢 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;
    }
}

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.

@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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文