Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit重复注释不会启动测试用例_Java_Junit - Fatal编程技术网

Java JUnit重复注释不会启动测试用例

Java JUnit重复注释不会启动测试用例,java,junit,Java,Junit,我有一个@Repeat注释,用于重复运行JUnit测试。代码取自此博客文章,并修改为使用JUnit4.10运行 Repeat.java @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface Repeat { int value(); } ExtendedRunner.java public class ExtendedRunner extends BlockJUnit4Cl

我有一个@Repeat注释,用于重复运行JUnit测试。代码取自此博客文章,并修改为使用JUnit4.10运行

Repeat.java

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Repeat {
    int value();
}
ExtendedRunner.java

public class ExtendedRunner extends BlockJUnit4ClassRunner {

    public ExtendedRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected Description describeChild(FrameworkMethod method) {
        if (method.getAnnotation(Repeat.class) != null &&
                method.getAnnotation(Ignore.class) == null) {
            return describeRepeatTest(method);
        }
        return super.describeChild(method);
    }

    private Description describeRepeatTest(FrameworkMethod method) {
        int times = method.getAnnotation(Repeat.class).value();

        Description description = Description.createSuiteDescription(
                testName(method) + " [" + times + " times]",
                method.getAnnotations());

        for (int i = 1; i <= times; i++) {
            Description d = Description.createSuiteDescription("[" + i + "] " + testName(method));
            d.addChild(Description.createTestDescription(getTestClass().getJavaClass(), testName(method)));
            description.addChild(d);
        }
        return description;
    }

    @Override
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {

        if (method.getAnnotation(Repeat.class) != null) {

            if (method.getAnnotation(Ignore.class) == null) {
                Description description = describeRepeatTest(method);
                runRepeatedly(methodBlock(method), description, notifier);
            }
            return;
        }

        super.runChild(method, notifier);
    }

    private void runRepeatedly(Statement statement, Description description,
                               RunNotifier notifier) {
        for (Description desc : description.getChildren()) {
            runLeaf(statement, desc, notifier);
        }
    }

}
当我运行测试时,我在事件日志中得到以下信息

Failed to start: 9 passed, 1 not started
这也是:


有趣的是,测试从0到9打印,这让我相信测试实际上运行了10次。然而,我得到了上面的事件日志。为什么会发生这种情况?

我在GitHub上发布了一条重复测试的
@规则。此解决方案比使用
运行程序
更灵活,因为它不排除使用另一个
运行程序

@ThreadSafe
@默认情况下,参数SarenonNullBy
公共类RepeatTest实现TestRule{
公共静态重复测试findRepeats(){
返回新的RepeatTest();
}
专用重复测试(){
超级();
}
@凌驾
公开声明适用(最终声明、说明){
返回新语句Repeater(语句、说明);
}
私有最终类语句Repeater扩展语句{
非公开最终声明;
私人最终整数重复计数;
专用语句Repeater(语句、说明){
超级();
这个。声明=声明;
Repeat=description.getAnnotation(Repeat.class);
this.repeatCount=getRepeatCount(repeat);
}
私有最终整数getRepeatCount(@Nullable Repeat Repeat){
整数计数=1;
如果(重复!=null){
count=repeat.count();
}
断言(“重复计数必须大于0”,计数,
排序比较。大于(0);
返回计数;
}
@凌驾
public void evaluate()可丢弃{
对于(int i=0;i
Failed to start: 9 passed, 1 not started
@ThreadSafe
@ParametersAreNonnullByDefault
public class RepeatTest implements TestRule {

public static RepeatTest findRepeats() {
    return new RepeatTest();
}

private RepeatTest() {
    super();
}

@Override
public Statement apply(final Statement statement, Description description) {
    return new StatementRepeater(statement, description);
}

private final class StatementRepeater extends Statement {

    private final Statement statement;
    private final int repeatCount;

    private StatementRepeater(Statement statement, Description description) {
        super();
        this.statement = statement;
        Repeat repeat = description.getAnnotation(Repeat.class);
        this.repeatCount = getRepeatCount(repeat);
    }

    private final int getRepeatCount(@Nullable Repeat repeat) {
        int count = 1;
        if (repeat != null) {
            count = repeat.count();
        }

        assertThat("Repeat count must be > 0", count,
                OrderingComparison.greaterThan(0));
        return count;
    }

    @Override
    public void evaluate() throws Throwable {
        for (int i = 0; i < repeatCount; i++) {
            statement.evaluate();
        }
    }
}
}