kotlin Maven故障保护:集成测试的NoClassDefFoundError

wwwo4jvm  于 2023-02-16  发布在  Kotlin
关注(0)|答案(2)|浏览(226)

设置:在 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是一个接口,是的,它是经过编译的,在maven的./target目录中有一个对应的.class文件,IntelliJ被设置为使用相同的文件。
不能完全弄清楚为什么这是失败的,并希望与插件的一些帮助。
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.xmlhttp://freetexthost.com/dbghyawijj
未通过集成测试步骤的完整日志:http://freetexthost.com/gbsmd3mwm2

jtoj6r0c

jtoj6r0c1#

面对同样的问题,我们通过添加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>
dpiehjr4

dpiehjr42#

我在将Sping Boot 项目迁移到JDK 17时遇到过这个问题。最后,pom.xmlfailsafe部分如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.2</version>
    <executions>
      <execution>
        <id>integration-test</id>
        <goals>
          <goal>integration-test</goal>
        </goals>
      </execution>
      <execution>
        <id>verify</id>
        <goals>
          <goal>verify</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <forkCount>1</forkCount>
      <reuseForks>false</reuseForks>
      <argLine>@{argLine}</argLine>
      <includes>
        <include>**/*IT.java</include>
      </includes>
      <classesDirectory>${project.build.outputDirectory}</classesDirectory>
      <trimStackTrace>false</trimStackTrace>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.22.2</version>
      </dependency>
    </dependencies>
  </plugin>

相关问题