使用java.lang.Exception的错误:测试类应该只有一个公共构造函数

问题描述 投票:0回答:1

我运行JUnit测试时遇到异常。

java.lang.Exception: Test class should have exactly one public constructor

以下是我的代码片段,有什么想法吗?

package com.tests;    

import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;


public class AllTests implements TestRule {
    private int retryCount;

    private AllTests(int retryCount) {
        this.retryCount = retryCount;
    }

    public Statement apply(Statement base, Description description) {
        return statement(base, description);
    }

    private Statement statement(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;

                // implement retry logic here
                for (int i = 0; i < retryCount; i++) {
                    try {
                        base.evaluate();
                        return;
                    }
                    catch (Throwable t) {
                        caughtThrowable = t;
                        System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
                    }
                }
                System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
                throw caughtThrowable;
            }
        };
    }

    @Rule
    public AllTests allTests = new AllTests(3);

    @ClassRule
    public static DockerComposeRule docker = wi.getDocker(logs);

    @Override
    public DockerComposeRule getDocker() {
        return docker;
    }

    @Test
    public void myFirstTest() throws Exception {
        // Test code here...
    }
}

我正在使用Gradle运行JUnit测试。这是一个Java项目,它直接失败。我尝试过很多东西,但无济于事。如果您愿意,很乐意提供更多详细信息。

java unit-testing exception junit
1个回答
0
投票

信息很明确:

java.lang.Exception:测试类应该只有一个公共构造函数

您将单元测试类与同一类中的Rule测试类合并:AllTests。 您应该在自己的类中定义规则并删除此构造函数:

private AllTests(int retryCount) {
    this.retryCount = retryCount;
}
© www.soinside.com 2019 - 2024. All rights reserved.