Executing ConsoleRunner from JUnit

Apart from command-line, it's perfectly possible to ConsoleRunner from inside a unit test.  This means you could have different JUnit tests (or some other framework) that run different cucumber features in parallel.

You can configure all settings and pass in system properties.  Inside your unit test you can only assert the exit code of the ConsoleRunner (only 0 is ok), but all logs and reports will still be written to the output directory specified.  Just make sure you configure each instance to log to a different directory.

This can be an alternative approach for Testing Spring Boot microservices using Cucumber and CWB REST that still lets you take advantage of the parallel execution and distribution of the ConsoleRunner.  Another great advantage of this approach is that if you start your Spring Boot application from within the unit test, code coverage should work out of the box.

Example JUnit tests launching ConsoleRunner
public class ITRunFeatures {

	...

    @Test
    public void desktopFeatures() throws Exception {
        Properties systemProperties = new Properties();
        systemProperties.setProperty("webServerPort", webServerPort.toString());

        ConsoleRunner consoleRunner = new ConsoleRunner("target/cucumber/desktop", "src/test/resources/features/");
        consoleRunner.setProperties(systemProperties);
        consoleRunner.setTags(new String[]{"~@ignore", "~@mobile"});
        consoleRunner.setNumberOfThreads(5);

        assertEquals(0, consoleRunner.start());
    }

    @Test
    public void mobileFeatures() throws Exception {
        Properties systemProperties = new Properties();
        systemProperties.setProperty("webServerPort", webServerPort.toString());

        ConsoleRunner consoleRunner = new ConsoleRunner("target/cucumber/mobile", "src/test/resources/features/");
        consoleRunner.setProperties(systemProperties);
        consoleRunner.setTags(new String[]{"~@ignore", "@mobile"});
        consoleRunner.setNumberOfThreads(5);

        assertEquals(0, consoleRunner.start());
    }
}