views:

464

answers:

3

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);
A: 

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.

monksy
That doesn't make any sense. Please post your code.
SingleShot
It doesn't make any sense. I do not have any overloaded methods that match assertArrayEquals(Object[],Object[]) within the reach of the method. The only place that the definition exists is within org.junit.Assert.
monksy
+1  A: 

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 {}
}
SingleShot
That's what I was doing, and the compile just didn't seem to want to point to the implementation in the org.junit.Assert.*; class.
monksy
Third time is a charm: please post your test code........
SingleShot
+1  A: 

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.

Jon Skeet
All of the assertArrayEquals were failing. This explanation makes the most sense.
monksy