mdewit
mdewit

Reputation: 2036

Junit not launching test case when it is an anonymous class

I'm trying to use JUnit for my functional testing. Basically I'm doing this to have access to the JUnit reports. Unfortunately I am running into a problem when trying to launch JUnit from a main method.

Basically I am developing a functional testing tool where the user can provide a test filename as a parameter from the command line. I've simplified it a bit below:

import org.junit.runner.JUnitCore;

public class MainClass {
    public static void main(String[] args) throws Exception {
        TestCase testCase = new TestCase() {
            @Override
            public String getPath() {
                return args[0];
            }
        };

        JUnitCore junit = new JUnitCore();
        junit.run(testCase.getClass());
    }
}

The TestCase class then acts on the supplied parameter and provides output:

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestCase {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestCase.class);

    public String getPath() {
        return "etc/Default.flow";
    }

    @Test
    public void testFunc() {
        try {
            LOGGER.info("Entered testFunc()");
            Launcher launcher = new Launcher(getPath());
            launcher.launch();
            launcher.awaitCompletion();

            Assert.assertTrue(launcher.getStatus());
            LOGGER.info("Success");
        } catch (AssertionError e) {
            LOGGER.error("Assertion error", e);
        }
    }

So from the above, we see that the Launcher instance will be launched with a different filename depending on what was entered on the command line.

The problem is however that Junit is not running my anonymous class. Basically the main method exits without any of the assertions or logging taking place. The TestCase testFunc() method is therefore not called at all.

However, when I change the TestCase instance to not be anonymous, everthing works as expected and the testcase is successful:

import org.junit.runner.JUnitCore;

public class MainClass {
    public static void main(String[] args) throws Exception {
        TestCase testCase = new TestCase();

        JUnitCore junit = new JUnitCore();
        junit.run(testCase.getClass());
    }
}

Why would JUnit launch the Test class only when it is not anonymous?

Upvotes: 1

Views: 491

Answers (1)

Evgeny
Evgeny

Reputation: 2568

If you add listener junit.addListener(new TextListener(System.out)); before running test you will see something like:

There were 2 failures:
1) initializationError(junit.MainClass$1)
java.lang.Exception: The class junit.MainClass$1 is not public.
...
2) initializationError(junit.MainClass$1)
java.lang.Exception: Test class should have exactly one public constructor
    at org.junit.runners.BlockJUnit4ClassRunner.validateOnlyOneConstructor(BlockJUnit4ClassRunner.java:158)
    at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:147)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)

It means that JUnit is unable to execute test cases represented by anonymous classes.

Upvotes: 2

Related Questions