java - Spring Boot Maven 集成测试给出 ClassNotFoundException
问题描述
我有一个带有两个应用程序的 Spring Boot 项目,一个在 src/test 中,一个在 src/main 中。我有两个应用程序,一个用于连接 SOAP 端点的中介,另一个充当用于测试的模拟服务器。
我有一个集成测试检查它是否会命中(需要测试应用程序(端口 9119)和主应用程序(端口 28433)启动)
我使用 maven spring boot 插件配置为在我的 POM 中运行这样的两个应用程序,并在运行 mvn verify 时使其跳过单元测试。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includePom>true</includePom>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
</execution>
<execution>
<id>pre-integration-test2</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
</execution>
<execution>
<id>post-integration-test2</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
当我运行 mvn verify 它在预集成阶段加载 28433 但是当它尝试在预集成阶段 2 运行 9119 时会出现此错误:
java.lang.ClassNotFoundException: com.nulogix.billing.mockserver.MockServerApp
at java.net.URLClassLoader.findClass (URLClassLoader.java:436)
at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:553)
at java.lang.Thread.run (Thread.java:835)
类路径目录是对的。我尝试在我的 pom 中添加一个类范围,但它似乎没有解决问题并给了我同样的错误。
在集成后阶段,当它关闭时,它会给出这个错误,我认为这只是意味着它无法加载应用程序上下文,因此没有找到关闭它的 bean
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.0.RELEASE:stop (post-integration-test2) on project billing_mediator: Spring application lifecycle JMX bean not found (fork is null). Could not stop application gracefully: org.springframework.boot:type=Admin,name=SpringApplication -> [Help 1]
当我运行 mvn -x 进行堆栈跟踪时,它会自动给我一个构建失败并给我这个
org.apache.maven.lifecycle.NoGoalSpecifiedException: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy.
这是否意味着我的 POM 设置错误?
解决方案
由于 maven 不扫描 src/test 文件夹,因此我只在我的主应用程序中添加了一个集成阶段来实现这一点。然后我创建了我的模拟服务器的应用程序上下文并让它在测试类之前运行。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {EndPointTestConfiguration.class
})
public class SoapIT {
private static ApplicationContext context;
@BeforeClass
static public void setup(){
SpringApplication springApplication = new SpringApplicationBuilder()
.sources(MockServerApp.class)
.build();
context = springApplication.run();
}
@Autowired
private String studyDetailDemo;
@Test
public void soapTest() throws ClientProtocolException, IOException {
String result = Request.Post("https://127.0.0.1:28433/nulogix/ws/billingtool")
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString("testing", ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
}
}
这将使我的主应用程序运行预集成测试,然后在测试运行之前启动我的模拟服务器应用程序。因此,这会在集成测试之前启动这两个应用程序并点击我的肥皂电话。
推荐阅读
- r - 如何使用 cumsum 但条件如果 <0 然后使用 0
- r - 将包含文本映射的列中的值转换为整数并将它们聚合
- python - 在 HS08TEST (Python 2.7) 上的问题代码上获取运行时错误 NZEC
- c# - asp.net gridView - 响应式网络表单
- javascript - JavaScript 对一系列 obj 进行分组
- java - 是否可以使用一些工具将 weblogic 及其域从 linux 复制到 windows?
- optimization - Cplex 优化——我的决策变量“无价值”
- swift - Swift Combine - 异步调用
- python - 用python对csv文件进行连续小波变换
- pic - MPLAB X IDE v5.45 低电压编程问题