Question

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);
Was it helpful?

Solution

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.

OTHER TIPS

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 {}
}

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.

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top