简体   繁体   English

Maven多模块项目中的JUnit测试抛出NoClassDefFoundError

[英]JUnit tests in Maven multi-module project throw NoClassDefFoundError

Our project has the following structure: 我们的项目具有以下结构:

├─ A
   ├─ A-A
   ├─ A-B
   ├─ A-C
   ├─ A-D
   ├─ A-E
   ├─ A-F
   ├─ A-G

The relevant JUnit tests are located in AA . 相关的JUnit测试位于AA中 Before we were using Maven, we had to adapt the run configuration (set the working directory in the arguments tab to AB which starts the application) in Eclipse in order to launch all tests without errors. 在使用Maven之前,我们必须在Eclipse中调整运行配置(将参数选项卡中的工作目录设置为AB ,这将启动应用程序),以便启动所有测试而不会出现错误。

Now we get a NoClassDefFoundError from a class located in AA when we try to run the tests, no matter if we do that in Eclipse (Run As > JUnit Test) or with Maven ( mvn test ). 现在,当我们尝试运行测试时,无论是在Eclipse中(运行方式> JUnit Test)还是在Maven中( mvn test )进行操作,我们都会从位于AA的类中获得NoClassDefFoundError I think this is different to similar issues on Stack Overflow because our tests aren't working at all. 我认为这与Stack Overflow上的类似问题不同,因为我们的测试根本无法正常工作。

My guess is that we have a bad configured POM and Surefire can't find all class files or something like that. 我的猜测是我们的POM配置不正确,Surefire找不到所有的类文件或类似的文件。 Is there a way to configure the plugin the way we did it with the run configuration before? 有没有一种方法可以像我们之前通过运行配置那样配置插件? Or is this error caused by something else (like M2Eclipse)? 还是此错误是由其他原因(例如M2Eclipse)引起的?

EDIT: The internal structure of project AA is as follows ( ClassCausingError extends a class from a Maven dependency): 编辑:项目AA的内部结构如下( ClassCausingError从Maven依赖ClassCausingError扩展了一个类):

├─ A-A
   ├─ src/main/java
       ├─ foo/bar/ClassCausingError.java
   ├─ src/main/resources
   ├─ src/test/java
       ├─ foo/bar/ClassCausingErrorTest.java
   ├─ target
   ├─ pom.xml

Setting the base directory in the main tab of the Maven build run configurations to AB fixed the issue. 将Maven构建运行配置的主选项卡中的基本目录设置为AB可解决此问题。 This seems to have the same effect as setting the working directory for JUnit previously. 这似乎与先前为JUnit设置工作目录具有相同的效果。 In addition I edited the POM to make the tests run from the command line: 此外,我还编辑了POM以使测试从命令行运行:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18</version>
    <configuration>
        <workingDirectory>A-B</workingDirectory>
    </configuration>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM