4

Spring boot and cucumber integration, when I do package this project become jar use mvn package command and produce advvic-1.0.jar. But, if I run this jar

java -jar target/advvic-1.0.jar

I get this error :

Caused by: cucumber.runtime.CucumberException: No backends were found. Please make sure you have a backend module on your CLASSPATH.

However, if I extract this jar, I found cucumber-java.jar on lib folder

this my pom :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.advvic.cucumber.spring</groupId>
    <artifactId>advvic</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>cucumber-spring-boot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>

        <spring-boot.version>1.4.1.RELEASE</spring-boot.version>

        <cucumber.version>1.2.5</cucumber.version>
        <selenium.version>3.0.1</selenium.version>
        <htmlunit.version>2.13</htmlunit.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>

        <!--Selenium server dependency is for version 3 and up -->
        <!--https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>${selenium.version}</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0-GA</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <configuration>
                            <executable>true</executable>
                            <attach>true</attach>
                        </configuration>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <executable>true</executable>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
sahmada
  • 317
  • 1
  • 3
  • 19
  • Why do you want to package the Cucumber jar with your Spring Boot application jar? Try setting the scope to the value "test" for all the cucumber-* dependencies in your pom.xml. – Thomas Traude Nov 11 '16 at 06:23
  • if I set the scope to test value, I have this error "java.lang.NoClassDefFoundError: cucumber/api/cli/Main" – sahmada Nov 11 '16 at 07:28
  • I took your pom.xml as posted (without adjusting the scope of the said dependencies to test) and created an Application class as described in the [Spring Boot Getting Started Guide](https://spring.io/guides/gs/spring-boot/). The Maven build is successful. And the Application starts without any errors when running `java -jar target/advvic-1.0.jar`. So it must be anything else in your code that you didn't posted here. Try isolating the problem by reducing your project files step by step. – Thomas Traude Nov 14 '16 at 21:17
  • I don't know Maven but a solution for Gradle is at https://stackoverflow.com/a/57741626/807037. – Noel Yap Aug 31 '19 at 21:16

2 Answers2

1

I had the exact same problem with a Spring Boot application that I wanted to use as a runner for Cucumber tests.

The problem is that Spring Boot repackages everything in its own way, thus making it hard for the default Cucumber ResourceLoaders to locate any cucumber.runtime.Backend classes as well as feature files and step definitions.

What I ended up doing was to implement a custom cucumber.runtime.io.ResourceLoader using Spring specific classes to search the classpath. I got the idea for that at https://stackoverflow.com/a/17283721/3216618

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String locationPattern = ... use path and suffix to 
Resource[] resources = resolver.getResources(locationPattern);

Finally you need to create an Iterable from the found org.springframework.core.io.Resource[]

Community
  • 1
  • 1
Marcus Wallin
  • 115
  • 1
  • 10
0

Maybe this can help you:

       <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <requiresUnpack>
                    <dependency>
                        <groupId>info.cukes</groupId>
                        <artifactId>cucumber-java</artifactId>
                    </dependency>
                </requiresUnpack>
            </configuration>
        </plugin>
Murphy Lan
  • 51
  • 3
  • The provided answer was flagged for review as a Low Quality Post. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – Trenton McKinney Sep 24 '19 at 00:55