首页 > 解决方案 > Cucumber + Java + Spring Boot = 从 jar 运行时没有后端错误

问题描述

这篇较早的帖子类似,我有一个使用 Spring boot 和 Cucumber 的 Java 应用程序。

这个应用程序的目的是使用 Selenium 来验证我的团队使用的一些工具,所以我的测试都在主要范围内。

测试在 IDE 中运行良好,但是当我打包我的 jar 并尝试运行它时,我得到没有找到后端。请确保您的 CLASSPATH 上有一个后端模块。

经过一番搜索,我配置了 spring-boot-maven-plugin 以解压 cucumber-java 依赖项。这修复了“无后端”错误,但是现在,当我运行 jar 时,我在控制台上打印了我的功能文件,但没有真正执行测试(它运行得太快了,我什至引入了一个错误,但它还是成功完成了.

我的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>

我的主要课程:

@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());
            }
        }
    }
}

测试之一?

@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 {

}

控制台结果:

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!

标签: javaspringmavenspring-bootcucumber

解决方案


推荐阅读