3

Similar to this older post I have a Java application using Spring boot and Cucumber.

This application's purpose is to use Selenium to validate some tools my team uses, so my tests are all in the main scope.

The tests run fine in the IDE, but when I package my jar and try running it, I get No backends were found. Please make sure you have a backend module on your CLASSPATH.

After some searching, I configured the spring-boot-maven-plugin to unpack the cucumber-java dependency. That fixed the "no backend" error, but now, when I run the jar, I get my feature files printed on the console but no tests really execute (It goes by too fast, and I even introduced an error but it finished successfully anyways.

My pom:

        <properties>
            <cucumber.version>4.7.1</cucumber.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>${cucumber.version}</version>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-spring</artifactId>
                <version>${cucumber.version}</version>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-java</artifactId>
                <version>${cucumber.version}</version>
            </dependency>

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.141.59</version>
            </dependency>
            <dependency>
                <groupId>com.codeborne</groupId>
                <artifactId>phantomjsdriver</artifactId>
                <version>1.4.4</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <requiresUnpack>
                            <dependency>
                                <groupId>io.cucumber</groupId>
                                <artifactId>cucumber-java</artifactId>
                            </dependency>
                        </requiresUnpack>
                    </configuration>
                </plugin>
            </plugins>
        </build>

My main class:

@SpringBootApplication
public class ExecutableMain implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ExecutableMain.class, args);
    }

    @Override
    public void run(String... args) {
        for (String arg : args) {
            ...
        }
        Result result = JUnitCore.runClasses(Test1.class, Test2.class, Test3.class);
        if (result.wasSuccessful()) {
            System.out.println("Success!");
        } else {
            System.out.println("Failure!");
            for (Failure failure : result.getFailures()) {
                System.out.println(failure.toString());
            }
        }
    }
}

One of the tests?

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "html:target/cucumber-test1", "json:target/cucumber-test1/cucumber.json"},
        glue = {"cucumber.test1", "cucumber.steps"},
        features = "src/main/resources/cucumber/test1/"
        )
public class Test1 {

}

Console result:

INFO 15724 --- [           main] cucumber.ExecutableMain                  : Starting ExecutableMain
INFO 15724 --- [           main] cucumber.ExecutableMain                  : No active profile set, falling back to default profiles: default
INFO 15724 --- [           main] cucumber.ExecutableMain                  : Started ExecutableMain in 4.546 seconds (JVM running for 8.234)
INFO 15724 --- [           main] c.runtime.java.ObjectFactoryLoader       : Loading ObjectFactory via service loader: io.cucumber.spring.SpringFactory
Feature: my test feature
  Validates stuff

 Scenario: my test scenario
    Given foo
    Then Bar
...
Success!

Saita
  • 964
  • 11
  • 36
  • @mpkorstanje I looked into it and a few other responses from other threads that touched the ResourceLoader subject, but I fail to see how to actually implement it. – Saita Sep 18 '19 at 18:04
  • I must have linked the wrong answer by accident. The right one: https://stackoverflow.com/a/46564059/3945473 You'd replace the contents of your `ExecutableMain.run` with a call to `CucumberStaticRunner.startTests` from the example. – M.P. Korstanje Sep 18 '19 at 19:57
  • I am having a hard time adapting the code. I am using version 4.7.1 and some things have changed. I managed to make it compile but when I run it I get: Caused by: java.lang.IllegalArgumentException: uri must have classpath scheme file:/C:/Users/.../cucumber/target/classes/cucumber/runtime/io/CucumberStaticRunner.class at io.cucumber.core.model.Classpath.resourceName(Classpath.java:22) ~[cucumber-core-4.7.1.jar:na] at cucumber.runtime.io.ResourceLoaderClassFinder.getClassName(ResourceLoaderClassFinder.java:39) ~[cucumber-core-4.7.1.jar:na] – Saita Sep 18 '19 at 20:44
  • You might have to fix the resource adaptor so the URI returned is a classpath URI. This won't be easy unfortunately. Cucumber was never designed to run inside a spring jar. – M.P. Korstanje Sep 18 '19 at 22:14

0 Answers0