Tomcat 集成测试完成后保留 运行

Tomcat Keeps Running After Integration Test Finished

如果您较早阅读本文,我很抱歉完全重写...

我将一个项目迁移到 Spring-Boot 项目,并在完成后执行 Tomcat 运行 中的集成测试结果。 运行 在 Eclipse 和 Maven 中也是如此。 Maven 的缺点是构建过程中断,只有 Ctrl+C 帮助,这实际上也停止了 Maven。

这是来自 pom.xml 的插件:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <source>${java.version}</source>
      <target>${java.version}</target>
    </configuration>
  </plugin>

  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
        <configuration>
          <finalName>at.a1.iap.spagat.aggregator</finalName>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.8</version>
    <executions>
      <execution>
        <id>integration-test</id>
        <goals>
          <goal>integration-test</goal>
        </goals>
        <configuration>
          <!-- required to make four soapui-test-classes work -->
          <forkMode>pertest</forkMode>
          <includes>
            <include>**/*ITest.java</include>
          </includes>
          <!-- no need to exclude and if you exclude no tests will be
            run <excludes> <exclude>**/*Test.java</exclude> </excludes> -->
        </configuration>
      </execution>
      <execution>
        <id>verify</id>
        <goals>
          <goal>verify</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>axistools-maven-plugin</artifactId>
    <version>1.4</version>
    <configuration>
      <mappings>
        <mapping>
          <namespace>http://www.agama.tv/ws/emp</namespace>
          <targetPackage>at.a1.iap.spagat.aggregator.external.agama</targetPackage>
        </mapping>
      </mappings>

      <sourceDirectory>${basedir}/src/main/resources/wsdl</sourceDirectory>
      <outputDirectory>${basedir}/src/gen/java</outputDirectory>
      <testCases>false</testCases>
      <serverSide>true</serverSide>
      <subPackageByFileName>false</subPackageByFileName>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>wsdl2java</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

经过大量摆弄后,我注意到这两个 bean 导致了这个(实际上只是其中一个):

  @Bean
  public ServletRegistrationBean servletWs(ServletContext servletContext) {
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);

    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ws/*");
    servletRegistrationBean.setLoadOnStartup(1);
    servletRegistrationBean.addInitParameter("dispatchOptionsRequest", "true");
    servletRegistrationBean.setName("general-dispatcher");

    return servletRegistrationBean;
  }

  @Bean
  public ServletRegistrationBean servletUi(ServletContext servletContext) {
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);

    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ui/*");
    servletRegistrationBean.setLoadOnStartup(1);
    servletRegistrationBean.setName("pagestuff-dispatcher");

    FilterRegistration.Dynamic welcomeFilter = servletContext.addFilter("WelcomeFilter", new WelcomeFilter("./ui/index"));
    welcomeFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/");

    return servletRegistrationBean;
  }

魔法线是

servletRegistrationBean.setLoadOnStartup(1);

如果我将其注释掉 Tomcat 将按预期停止。那么为什么我不应该这样做,我应该怎么做呢?

如果您需要进一步的代码摘录或其他信息,请随时询问。

这是 Spring-Boot 1.4.1 with embedded Tomcat

我不知道我是否应该 post 这没有假设闪电击中我或 Pivotal 的全体员工都禁止我...

无论如何,我 "solved" 通过在集成测试中添加一个确定代码是否 运行 的方法来做到这一点:

  public static boolean isInRunningITest() {
    return Stream.of(Package.getPackages())
        .map(Package::getName)
        .anyMatch(name -> StringUtils.startsWithAny(name, "org.springframework.test", "org.springframework.boot.test"));
  }

显然它会检查 Spring 和 Spring-Boot,其他任何内容都被排除在外。所以剩下的步骤是将 setLoadOnStartup() 包装在 if ().

这对我有用,可能是我编写过的最脏的东西之一...