简体   繁体   中英

Cucumber test for Spring Boot can run in “mvn test” but not in “mvn verify”

Summary:

I am running some tests using Cucumber against a Spring Boot application. My Cucumber test are running fine when I execute them using "mvn test" but fails when I execute them in the "mvn verify" lifecycle.

Details:

My Cucumber runner class looks like this:

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

}

when I execute "mvn test", I can see in the logs that the Cucumber runner is instantiated before the maven spring boot plugin instantiates the Spring Boot instance:

-------------------------------------------------------
 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)

I know that my Cucumber test is actually an integration test so I move it to run as part of the "mvn verify" lifecycle phase instead by renaming it to CucumberRunnerIT.java and configuring the pom.xml as follows:

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

When I run it as part of "verify" though, I get the following error:

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

I noticed that because I had set maven spring-boot plugin to run during pre-integration-test, it is executed before Cucumber is initialized but I'm not sure if this is the error. I did try to configure spring-boot plugin to start the app during "integration-test" instead of "pre-integration-test" but it didn't seem to do much.

Any ideas on what I'm doing wrong here?

This is jdk version related issue. If you use JDK 1.8.0u51, then it will be solved.

If your jdk version is higher, then this problem will occur. So please use JDK 1.8.0u51 or lower version.

For more, you can go through this issue. It is same to your issue: https://github.com/cucumber/cucumber-jvm/issues/912

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM