簡體   English   中英

java.lang.Exception錯誤:測試類應該恰好具有一個公共構造函數

[英]ERROR with java.lang.Exception: Test class should have exactly one public constructor

運行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.lang.Exception:測試類應僅具有一個公共構造函數

您將單元測試類和Rule測試類合並到同一類: AllTests
您應該在自己的類中定義規則並刪除此構造函數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM