【问题标题】:Tomcat Keeps Running After Integration Test Finished集成测试完成后Tomcat继续运行
【发布时间】:2017-05-24 22:08:25
【问题描述】:

很抱歉,如果您之前读过这篇文章,我会完全重写......

我将一个项目迁移到一个 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 会按预期停止。那么为什么我不应该这样做,我应该怎么做呢?

如果您需要更多代码摘录或其他信息,请随时提出。

这是带有嵌入式 Tomcat 的 Spring-Boot 1.4.1

【问题讨论】:

    标签: tomcat spring-boot integration-testing


    【解决方案1】:

    我不知道我是否应该在不假设闪电会随时击中我的情况下发布此消息,或者 Pivotal 的全体员工都禁止我...

    无论如何,我通过添加一个确定代码是否在集成测试中运行的方法来“解决”这个问题:

      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 () 中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多