Search code examples
javaeclipsemavenjunitsurefire

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 A-A. Before we were using Maven, we had to adapt the run configuration (set the working directory in the arguments tab to A-B which starts the application) in Eclipse in order to launch all tests without errors.

Now we get a NoClassDefFoundError from a class located in A-A 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 A-A 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

Solution

  • Setting the base directory in the main tab of the Maven build run configurations to A-B 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>