Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
64 views
in Technique[技术] by (71.8m points)

java - Using assertArrayEquals in unit tests

My intention is to use assertArrayEquals(int[], int[]) JUnit method described in the API for verification of one method in my class.

But Eclipse shows me the error message that 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?

question from:https://stackoverflow.com/questions/3457941/using-assertarrayequals-in-unit-tests

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...