Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse/Maven: JUnit tests not compiled when running them

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

like image 243
Tom Avatar asked May 22 '11 11:05

Tom


People also ask

How do I run a Maven JUnit test in Eclipse?

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests. Hint: By default surefire looks for files with *Test.

Does Maven test compile?

mvn test-compile: Compiles the test source code. mvn test: Runs tests for the project. mvn package: Creates JAR or WAR file for the project to convert it into a distributable format. mvn install: Deploys the packaged JAR/ WAR file to the local repository.

How do I run a JUnit project in Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

Why my JUnit is not showing in Eclipse?

Click on Libraries tab. Click on Add Library button. Click on JUnit and Next button. Choose JUnit version.


2 Answers

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 to target/test-classes:

  1. Right click the src/test/java folder in the Package Explorer
  2. Select Build Path -> Configure Output Folder
  3. Enter target/test-classes, click OK

Now 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 from test-classes.

There are a few duplicates to this question:

  • ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven)
  • Eclipse doesn't see my new junit test
  • junit not using the newest file
like image 56
apa64 Avatar answered Nov 03 '22 22:11

apa64


In addition to the answer below

  1. Right click the src/test/java folder
  2. Select Build Path -> Configure Output Folder
  3. Enter target/test-classes, click OK

you should check to ensure that your builder is setup correctly by right clicking your project and going to Properties -> Builder. If you see that your builder is missing, you need to install one. In my case, the maven project had an AspectJ dependency and when I used the Maven Eclipse plugin to build my Eclipse project, it was looking for an AspectJ builder by default. I installed the AspectJ development tools and it solved the problem.

Hope this helps!

like image 34
Roosh Avatar answered Nov 03 '22 22:11

Roosh