且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven Junit测试"noClassDefFoundError";

更新时间:2021-09-26 05:56:53

旧问题,但我会提供答案,因为前几天我遇到了一个非常相似的问题. 我在project.model包中为Foo类编写了单元测试,该包在project.cache包中具有Bar类型的成员.当maven运行单元测试时,我在创建Bar类型的模拟程序时遇到了以上错误.

Old question but I'll provide my answer since I had a very similar issue the other day. I was writing a unittest for class Foo in package project.model which had a member of type Bar in package project.cache. When maven were to run the unittests, I got the above error on creating a mock of type Bar.

运行mvn verify -X时,我注意到运行测试时类路径中缺少目标/类.

When running mvn verify -X, I noticed that target/classes was missing from the classpath when running the test.

我通过在pom中添加以下内容解决了该问题.

I solved it by adding the following to the pom.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.0</version>
      <configuration>
        <additionalClasspathElements>
          <additionalClasspathElement>${project.basedir}/target/classes</additionalClasspathElement>
        </additionalClasspathElements>
      </configuration>
    </plugin>

  </plugins>
</build>