完成集成测试后Tomcat继续运行

时间:2017-05-24 22:08:26

标签: tomcat spring-boot integration-testing

如果您之前阅读此内容,我很抱歉完全重写...

我将项目迁移到Spring-Boot项目,并在Tomcat完成后运行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

1 个答案:

答案 0 :(得分:0)

我不知道是否应该发布此信息而不假设闪电击中任何一秒钟或者Pivotal的全体员工禁止我......

无论如何,我已经解决了#34;这可以通过添加一个方法来确定代码是否在集成测试中运行:

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

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