【发布时间】:2018-07-18 09:28:29
【问题描述】:
我在运行 JUnit 测试时遇到异常。
java.lang.Exception: Test class should have exactly one public constructor
下面是我的代码 sn-p,有什么想法吗?
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