Spring 使用 Cucumber E2E (Selenium) 测试启动

Spring Boot with Cucumber E2E (Selenium) testing

在我的项目中,我必须对当前的 Maven 模块进行设置:

- Application 
|- (application code, using Spring Boot 1.2.6)

- E2E-testing (has a dependency to Application)
|- src
    |- main
        |- java
            |- AbstractCucumberTest.java

以前,这曾经是一个 Spring 3 应用程序服务 JSP 页,因此不包括启动。我将其重构为一个引导应用程序。

E2E 测试设置基本上构建了一个 WAR 应用程序代码文件,E2E 模块启动了一个 Jetty 服务器,运行 宁 WAR。一切都好。 现在,在重构之后,没有那么多了。

Jetty 设置不再按原样工作。当我启动 WAR 时,我收到 class 加载异常,这与 Jetty 本身有关。现在,我没有挂在 Jetty 上,它对我来说只是一个测试容器。所以我开始深入研究 Boot 文档,因为我确信必须有一种方法可以使它全部“Boot'-iful,意思是:我可以在启动测试时简单地启动应用程序。当然,还有,所以我在我的 AbstractCucumberTest 上得到了这些注释:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class, loader = SpringApplicationContextLoader.class)
@WebIntegrationTest({"spring.profiles.active=local"})
public abstract class AbstractCucumberTest extends SeleniumTest {

所以,基本上,我正在加载应用程序的应用程序配置,并从测试中启动 Tomcat 服务器。所有人都很高兴,因为现在应用程序在启动 Cucumber 测试时成功启动。应用程序启动,它读取 class 路径,Liquibase 启动,一切正常。我可以调用 Actuator 端点,一切正常。甚至应用程序的 REST 端点都在工作,所以 Spring MVC 正在做它的事情。

但是,有 1 件事不是,那是为 JSP 服务的 -- 这当然是 E2E 测试的一个交易破坏者。每次浏览网页时,我都会遇到同样令人不安的 404 错误。在你问之前:是的,tomcat-embed-jasperjstl 存在。它们存在于应用程序中,我什至将它们添加到 E2E pom 中,但运气不佳。事实上,这些是我尝试过的,但都失败了:

在我看来,应用程序找不到 JSP。其他一切都很好,只是 JSP 不是 found/served。

有没有人有什么想法?

哦,顺便说一句,我尝试使用 spring-boot-maven-plugin 来尝试以这种方式启动应用程序。但是,问题是它不能分叉。在 1.2.6 版本中,fork 只是……不 fork。当我说 fork 时,我指的是 Jetty 方式:它启动应用程序,并将控制权交还给 Maven。它实例化了一个 "stop" 命令,Maven 可以在所有测试都具有 运行 之后调用该命令。 在 1.3.0.BUILD-SNAPSHOT version 中,它应该存在(使用开始目标),但这对我仍然不起作用。

[编辑] 对于它的价值,应用程序模块被配置为构建一个 WAR 文件。所以它是不是 JAR 包装。

对于可能关心的人,最终我转向了 Maven Cargo 插件,它工作得很好。我是在E2E测试模块的pom中配置的。如您所见,我的应用程序被标记为可部署,cargo 插件在预集成测试阶段启动,并在 post-集成测试阶段关闭。

<plugin>
  <groupId>org.codehaus.cargo</groupId>
  <artifactId>cargo-maven2-plugin</artifactId>
  <version>1.4.16</version>
  <configuration>
    <container>
        <containerId>tomcat8x</containerId>
        <zipUrlInstaller>
            <url>
                http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.27/bin/apache-tomcat-8.0.27.zip
            </url>
        </zipUrlInstaller>
    </container>
    <deployables>
        <deployable>
            <groupId>my.group.id</groupId>
            <artifactId>Application</artifactId>
            <type>war</type>
            <properties>
               <context>/context</context>
            </properties>
        </deployable>
    </deployables>
    <configuration>
        <type>standalone</type>
        <properties>
            <cargo.servlet.port>9999</cargo.servlet.port>
            <cargo.jvmargs>-Dspring.profiles.active=local</cargo.jvmargs>
        </properties>
    </configuration>
  </configuration>
  <executions>
    <execution>
        <id>start-container</id>
        <phase>pre-integration-test</phase>
        <goals>
            <goal>start</goal>
        </goals>
   </execution>
   <execution>
        <id>stop-container</id>
        <phase>post-integration-test</phase>
        <goals>
            <goal>stop</goal>
        </goals>
   </execution>
 </executions>
</plugin>