views:

48

answers:

3

My intention is to use assertArrayEquals Junit method described out there: [http://www.junit.org/apidocs/org/junit/Assert.html#assertArrayEquals(int[], int[])][1]

for verification of one method in my class. But it happened Eclipse shows me the error message it can't recognize such a method. Those two imports are in place:

import java.util.Arrays;
import junit.framework.TestCase;

Did I miss something?

Thank you!

[1]: http://www.junit.org/apidocs/org/junit/Assert.html#assertArrayEquals(int[], int[])

A: 

Try adding
import static org.junit.Assert.*;

assertArrayEquals is a static method.

+2  A: 

This should work with JUnit 4:

import static org.junit.Assert.*;
import org.junit.Test;

public class JUnitTest {

    /** Have JUnit run this test() method. */
    @Test
    public void test() throws Exception {

        assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3});

    }
}

(answer is based on this wiki article)


And this is the same for the old JUnit framework (JUnit 3):

import junit.framework.TestCase;

public class JUnitTest extends TestCase {
  public void test() {
    assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3});
  }
}

Note the difference: no Annotations and the test class is a subclass of TestCase (which implements the static assert methods).

Andreas_D
It is JUnit3, but I'm getting the message: Description Resource Path Location TypeThe method assertArrayEquals(int[], int[]) is undefined for the type DeckTest
AndroidNoob
are you extending TestCase? Please post sample code that shows the class in question.
matt b
@AndoidNoob - Assert@assertArrayEquals has been introduced with JUnit 4. So you either have to switch to JUnit 4 (always recommended) or verify the equality of arrays with several Java statements (loop through the array after making sure, they're of the same size)
Andreas_D
A: 

If you are writing JUnit 3.x style tests which extend TestCase, then you don't need to use the Assert qualifier - TestCase extends Assert itself and so these methods are available without the qualifier.

If you use JUnit 4 annotations, avoiding the TestCase base class, then the Assert qualifier is needed, as well as the import org.junit.Assert. You can use a static import to avoid the qualifier in these cases, but these are considered poor style by some.

mdma