在单元测试中使用 assertArrayEquals [英] Using assertArrayEquals in unit tests

查看:26
本文介绍了在单元测试中使用 assertArrayEquals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意图是使用API 用于验证我班级中的一种方法.

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

但是 Eclipse 向我显示了它无法识别这种方法的错误消息.这两个导入已到位:

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;

我错过了什么吗?

推荐答案

这适用于 JUnit 5:

import static org.junit.jupiter.api.Assertions.*;

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

这应该适用于 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});
 
    }
}


旧的 JUnit 框架 (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});
  }
}

注意区别:没有注解,测试类是 TestCase 的子类(实现静态断言方法).

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

这篇关于在单元测试中使用 assertArrayEquals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆