java - JunitParamsRunner 测试

标签 java junit junit4

我尝试运行以下代码,但收到参数数量错误错误。

package Homework7;
import static junitparams.JUnitParamsRunner.$;
import static org.junit.Assert.*;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JUnitParamsRunner.class)

public class Problem3TestClass {
    private Problem3Class p3class;

    @SuppressWarnings("unused")
    public static final Object[] Problem3TestClass(){
        return $(
//              Parameters are: (1, 2)
//              1 = x,2 = y             
//              Test case 1
                $(12223,1)


                );
    }
    @Before
    public void setUp() {
        p3class = new Problem3Class();
    }

    @Test
    @Parameters(method = "Problem3TestClass")
    public void test(int[] x,int y)
    {
        assertEquals(y,p3class.countRepeated(x));
    }

}

我的 countRepeated 方法通过以下方式调用

public int countRepeated (int[] x) 

我在这里做错了什么?

最佳答案

根据source中的评论

JUnitParamsRunner#$方法

Should not be used to create var-args arrays, because of the way Java resolves var-args for objects and primitives.

因此尝试更改测试方法以接受 List<Integer>而不是int[] 。 下面的代码应该可以工作。

@SuppressWarnings("unused")
public static final Object[] Problem3TestClass() {
    List<Integer> x = new ArrayList<Integer>();
    int y = 2;
    return $(x, y);
}

@Test
@Parameters(method = "Problem3TestClass")
public void test(List<Integer> x, int y) {
    // update countRepeated to accept List<Integer> or do some conversion here
    assertEquals(y, p3class.countRepeated(x));
}

关于java - JunitParamsRunner 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647313/

相关文章:

java - Atomikos:使用 PostgreSQL 时数据未保存

java - Maven 无法在 : 中找到 Javac 编译器

java - JMockit 在单元测试中保持不变

java - 如何使用isA-Matcher

java - JUnit 测试用例与 tomcat 容器或 JVM 一起运行

java - Java 中的 MySQL 模拟器

java - J8583:ISO8583 MessageFactory 没有消息类型 0800 的解析指南

java - JUnit 中的多个规则

java - 如何从代码中运行整个 JUnit 测试套件

java - 为什么需要 `robot.keyRelease(KeyEvent.VK_CONTROL)`?