gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:20:45 25 4
gpt4 key购买 nike

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

我将一个项目迁移到一个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

最佳答案

我不知道我是否应该在没有假设闪电击中我或 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 () 中。

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

关于集成测试完成后tomcat继续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44169265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com