0

I am getting an exception when I run my JUnit tests.

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

Below is my code snippet, any ideas?

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...
    }
}

I am running a JUnit test, with Gradle. It is a Java project and it fails straightaway. I have tried numerous things but to no avail. Happy to provide more details if you want.

Saffik
  • 911
  • 4
  • 19
  • 45

1 Answers1

0

The message is clear :

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

You merged the unit test class with the Rule test class in the same class : AllTests.
You should define your rule in its own class and remove this constructor :

private AllTests(int retryCount) {
    this.retryCount = retryCount;
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Then i get the following: `java.lang.Exception: Test class should have exactly one public zero-argument constructor` – Saffik Jul 18 '18 at 09:30
  • https://stackoverflow.com/questions/8295100/how-to-re-run-failed-junit-tests-immediately – Saffik Jul 18 '18 at 09:35
  • Changing to no arg constructor causes further failure: `public AllTests allTests = new AllTests(3);` – Saffik Jul 18 '18 at 09:36
  • But you don't have to provide a arg in the test class. In the referenced question, it is not the constructor of the test class that has a arg, it is the inner class that is a Rule. – davidxxx Jul 18 '18 at 09:38
  • Ah I see, in that case - It won't work for me because I have some static declarations and inner class cannot have static declarations. Thanks for your help though, appreciate it! – Saffik Jul 18 '18 at 09:43