Spring Boot的黄瓜测试可以在“ MVN测试”中运行,但不能在“ MVN验证”中运行

fei*_*pet 5 java cucumber maven spring-boot cucumber-java

摘要:

我正在对Spring Boot应用程序使用Cucumber运行一些测试。当我使用“ mvn测试”执行它们时,我的Cucumber测试运行良好,但是在“ mvn verify”生命周期中执行它们时,我的黄瓜测试失败。

细节:

我的黄瓜赛跑者课程如下所示:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/features/creditCardSummary.feature"},
        glue = {"th.co.scb.fasteasy.step"},
        plugin = {
                "pretty",
                "json:target/cucumber-json-report.json"
                }
)

public class CreditCardRunnerTest {

}
Run Code Online (Sandbox Code Playgroud)

当我执行“ mvn test”时,我可以在日志中看到在maven spring boot插件实例化Spring Boot实例之前,实例化了CucumberRunner:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running CreditCardRunnerTest
@creditcards
Feature: POST /creditcards/summary
  As a user, I would like to get basic information, balance and/or last 'n' transactions of a given list of credit cards, so that I can provide the information for further operations.

...........

2016-11-13 07:29:02.704  INFO 14716 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090 (http)
2016-11-13 07:29:02.721  INFO 14716 --- [           main] t.c.s.fasteasy.step.CreditCardStepdefs   : Started CreditCardStepdefs in 13.486 seconds (JVM running for 16.661)
Run Code Online (Sandbox Code Playgroud)

我知道我的Cucumber测试实际上是一个集成测试,因此我将其移动为“ mvn verify”生命周期阶段的一部分,而不是将其重命名为CucumberRunnerIT.java并按如下方式配置pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <argLine>${surefireArgLine}</argLine>
                <excludes>
                    <exclude>**/IT.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
                <execution>
                    <id>integration-tests</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <argLine>${failsafeArgLine}</argLine>
                        <skipTests>${skip.integration.tests}</skipTests>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

当我将其作为“验证”的一部分运行时,出现以下错误:

2016-11-13 07:39:42.921  INFO 12244 --- [lication.main()] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-11-13 07:39:43.094  INFO 12244 --- [lication.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090 (http)
2016-11-13 07:39:43.099  INFO 12244 --- [lication.main()] th.co.scb.fasteasy.Application           : Started Application in 10.41 seconds (JVM running for 52.053)
[INFO]
[INFO] --- maven-failsafe-plugin:2.19.1:integration-test (default) @ creditcards ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running CreditCardRunnerIT
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.716 sec <<< FAILURE! - in CreditCardRunnerIT
initializationError(CreditCardRunnerIT)  Time elapsed: 0.008 sec  <<< ERROR!
cucumber.runtime.CucumberException: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy


Results :

Tests in error:
  CreditCardRunnerIT.initializationError ? Cucumber java.lang.ArrayStoreExceptio...

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO]
[INFO] --- maven-failsafe-plugin:2.19.1:integration-test (integration-tests) @ creditcards ---
Run Code Online (Sandbox Code Playgroud)

我注意到,因为我已将maven spring-boot插件设置为在集成前测试期间运行,所以该插件在初始化Cucumber之前执行,但是我不确定这是否是错误。我确实尝试将spring-boot插件配置为在“集成测试”而不是“集成前测试”期间启动应用程序,但是它似乎并没有做什么用。

对我在这里做错的任何想法吗?

Sky*_*ker -1

这是jdk版本相关的问题。如果你使用JDK 1.8.0u51,那么就可以解决。

如果你的jdk版本较高,那么就会出现这个问题。所以请使用JDK 1.8.0u51或更低版本。

想了解更多,大家可以去看看这个问题。这与您的问题相同:https ://github.com/cucumber/cucumber-jvm/issues/912