I am working on a project using Maven and Eclipse (m2eclipse plugin). I've got problems with the JUnit tests:
Sometimes, when running them within Eclipse, they wont be compiled, but the old class files are used instead. When I delete the class files, I get ClassNotFoundExceptions
in Eclipse. I then have to manually recompile them by using mvn test-compile
or other goals.
I also noticed that the class files of the tests sometimes are put into the classes
subdirectory instead of test-classes
.
I really can't figure out what is wrong.
The JUnit java files are within src/main/java
and are correctly named (*Test.java
).
Do I have to compile and run them always via Maven? Why doesn't Eclipse compile the files when I want to run them? (Interestingly, sometimes it does. Sometimes everything works perfectly.)
Best Answer
I had the same problem with STS Eclipse (Spring development variant), m2e and JUnit. The solution was to set the output folder for
src/test/java
totarget/test-classes
:src/test/java
folder in the Package Explorertarget/test-classes
, click OKNow the changes in test classes are compiled correctly and you should be able to run JUnit tests in Eclipse.
The problem is that Eclipse compiles the unit tests to the default output folder
target/classes
while JUnit plugin correctly tries to run them fromtest-classes
.There are a few duplicates to this question: