1

I am using Junit-4 to run some parameterized tests inside of Eclipse Luna. I can run a single class by right-clicking and selecting "run as -> JUnit Test", but if I expand the class and do the same action on a single method (to run only one test), I get an initialization error inside "Unrooted Tests". However, if I run the entire class worth of methods and then right-click on a single test in the results pane and click "run", it runs just fine.

How can I set up JUnit so that I can run a single test method without having to run the whole class first? What configuration options should I be looking at? It's a maven project, but I'm not sure that matters. Stepping through, I can see the issue occurs when JUnit attempts to run the test, after the parameters are generated successfully, but I cannot see the source code at that point, so my information is very limited. No error/exception details are shown :(

I'm pretty sure it's a configuration problem because I managed to reproduce the problem with the following test case:

@RunWith(Parameterized.class)
public class JUnitTesting {
    /**
     * Generates list of environments to use for the test.
     * @return The list of environments to pass to the constructor.
     */
    @Parameters
    public static synchronized  Collection<String[]> environmentsToUse() {
            LinkedList<String[]> environments = new LinkedList<String[]>();
            environments.add(new String[]{"WINDOWS", "firefox", null});
            environments.add(new String[]{"WINDOWS", "chrome", null});
            return environments;    
    }

    public JUnitTesting(String os, String browser, String version) {

    }

    @Test
    public void sampleTest() {
        System.out.println("Test run.");
    }
}
Yamikuronue
  • 746
  • 8
  • 37
  • It occurs on any test method I try, I suspect it's a project configuration problem. Eclipse Luna 4.4 – Yamikuronue Nov 13 '14 at 13:56
  • @E-Riz As for code, what are you looking for? My test classes extend from a base test class that is kind of long, do you want the parameterization generator? The setup? The logging and test watcher? What would be useful? – Yamikuronue Nov 13 '14 at 13:58
  • Possible duplicate, can you please check? http://stackoverflow.com/questions/12798079/initializationerror-with-eclipse-and-junit4-when-executing-a-single-test – dubes Nov 13 '14 at 14:06
  • I didn't originally notice that you're talking about Parameterized tests. Could be a bug, or just a limitation because of the way parameters interact with normal test lifecycle. If you can reduce it down to a simple test that demonstrates the problem, that would help. – E-Riz Nov 13 '14 at 14:07
  • @dubes I can't tell what the problem was there or what the purported solution is fixing. – Yamikuronue Nov 13 '14 at 14:08
  • @E-Riz Added test case – Yamikuronue Nov 13 '14 at 14:16

1 Answers1

4

It was a known limitation of Eclipse's JUnit support in versions before Mars (4.5). Apparently also in IntelliJ. If you can't use a Mars milestone or release build, there is a workaround, if you're willing to use your own custom runner.

Community
  • 1
  • 1
E-Riz
  • 31,431
  • 9
  • 97
  • 134