设置:Spring Boot 应用程序中的 Kotlin 集成测试。
当我使用 IntelliJ 运行测试时,测试工作正常。 但是,当我尝试使用 Maven 运行集成测试时:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.19.1:integration-test (default) on project myapp: Execution default of goal org.apache.maven.plugins:maven-failsafe-plugin:2.19.1:integration-test failed: There was an error in the forked process
[ERROR] java.lang.NoClassDefFoundError: ch/cypherk/myapp/service/CustomerService
[ERROR] at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
[ERROR] at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3138)
[ERROR] at java.base/java.lang.Class.getConstructor0(Class.java:3343)
[ERROR] at java.base/java.lang.Class.getConstructor(Class.java:2152)
[ERROR] at org.apache.maven.surefire.junit.PojoAndJUnit3Checker.isPojoTest(PojoAndJUnit3Checker.java:51)
...
这很烦人,因为我有点需要竹子来运行该死的东西。
CustomerService
是一个接口(interface),是的,它是编译的,maven 的 ./target
目录和 IntelliJ 中有一个相应的 .class
文件设置为使用相同的。
无法完全弄清楚为什么会失败,希望能为插件提供一些帮助。
pom.xml
的相关摘录:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<!-- doesn't make a difference if `true` or `false`-->
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*IT*</include>
</includes>
<excludes>
<exclude>**/ui/*</exclude>
</excludes>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
完整的 pom.xml
:http://freetexthost.com/dbghyawijj
失败的集成测试步骤的完整日志:http://freetexthost.com/gbsmd3mwm2
最佳答案
遇到同样的问题,通过添加 target/classes 作为额外的类路径元素解决了这个问题。感觉有点脏但有效:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
关于java - Maven 故障保护 : NoClassDefFoundError for integration tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55609640/