有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JUnit自定义运行程序不工作?

我正在尝试编写一个定制的Runner,它只是按照随机顺序进行测试。跑步者:

public class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
    public TestClassRunnerForParameters(Class<?> type) throws Exception {
        super(type);
    }

    protected java.util.List<org.junit.runners.model.FrameworkMethod> computeTestMethods() {
        java.util.List<org.junit.runners.model.FrameworkMethod> methods = super
                .computeTestMethods();
        Collections.shuffle(methods);
        return methods;
    }
}

现在,如果不是参数化测试,它可以正常工作。是否可以使用参数测试来实现这一点?实现参数化接口


共 (2) 个答案

  1. # 1 楼答案

    只需添加一个构造函数(按照建议):

    public TestClassRunnerForParameters(Class testClass) {
       ...
    }
    

    并将其委托给构造函数。在这种情况下,构造函数应该是public,因为JUnit/Surefire正在使用反射

  2. # 2 楼答案

    我得说,这个错误是相当自我描述的:

    Custom runner class TestClassRunnerForParameters should have a public constructor with signature TestClassRunnerForParameters(Class testClass)

    您的类没有具有该签名的构造函数。它唯一的构造函数有参数Class<?> typeList<Object[]> parameterListint i。你应该去掉后两个论点。另外,这个构造器不是public;你应该在它前面加public

    此外,如果您试图构建参数化测试,可能会在org.junit.runners.Parameterized运行程序中进行测试,因为它正是这样做的。这是一个好主意