AssertArrayEquals (Object [] o1, Object [] o2) could not be found
My setup:
Netbeans 6.7
Java6
JUnit 4.5 added as the Test libraries
When I try to pass two arrays of classes (cast as Object []), I get a "can't find character" error and my test case won't compile.
I have no problem with other assert statements, and as I said, I am using JUnit 4.5 libraries.
Does anyone know how to fix this, or have you observed this quirky behavior?
Netbeans can find this function declaration through its autocomplete, but cannot find where it is, or can go to sources.
Sample code:
CustomObject[] coa = { new CustomObject() ....}
CustomObject[] expected = { new CustomObject() ... }
assertArrayEquals((Object[])coa, (Object[])expected);
source to share
Well, Assert.assertArrayEquals is a static method, as you can see from your code that works:
org.junit.Assert.assertArrayEquals(....)
But in the code you gave, you tried to use it as an instance method:
assertArrayEquals((Object[])coa, (Object[])expected);
This will only work if you have statically imported Assert.*
or Assert.assertArrayEquals
.
Now, if your other assertions work, I am assuming that you are still pulling from TestCase
(ie the "old" way of writing JUnit tests) and that your assertions are being called TestCase.assertEquals
, etc.
If you could give a short but complete unit test example where one assertion works but assertArrayEquals
doesn't, we could probably work out what's going on.
source to share
You don't need to fully qualify a statement or create arrays of arrays of objects. Just import the correct JUnit parts and pass your arrays directly. You should reverse the order of the parameters from your example, although - what you expect is first ("expected"), what you actually get from the test is second ("actual"). This works great:
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 {}
}
source to share
I like SingleShot's answer, except that its two arrays contain the same objects. What if the objects are not the same actual objects (different objects have the same values, but must be equal).
So I thought I would strengthen his answer to show you 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;
}
}
source to share