简体   繁体   中英

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 . 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.

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 ). I think this is different to similar issues on Stack Overflow because our tests aren't working at all.

My guess is that we have a bad configured POM and Surefire can't find all class files or something like that. 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)?

EDIT: The internal structure of project AA is as follows ( ClassCausingError extends a class from a Maven dependency):

├─ 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. This seems to have the same effect as setting the working directory for JUnit previously. In addition I edited the POM to make the tests run from the command line:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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