17

I'm using eclipse to run the tests in a single junit(4) test class. The tests in the class all run just fine. Then I add an additional test and run the class through the test running in ecplise again. Only the old tests are run. The new test isn't seen by eclipse. There's no error or anything, it's just as if eclipse is looking at an old version of the test.

If I run the tests using maven, everything works fine. Additionally, after I run the tests in maven, ecplipse can see and run the new test correctly.

Any ideas what's going on? Any ideas how to get ecplipse's test runner to see my new test cases?

morgancodes
  • 25,055
  • 38
  • 135
  • 187

10 Answers10

9

I had the same issue. I solved it by doing the following:

  • Going to Project -> Properties -> Java Build Path
    For the source folder src/test/java, the output folder was set to "Default output folder"
  • Setting this to the typical Maven target/test-classes directory in your Maven structure

After this, Maven and Eclipse were in sync (as opposed to Eclipse happily running an older version of the tests, from whenever the last Maven compile was).

rolve
  • 10,083
  • 4
  • 55
  • 75
Ryan Dawe
  • 99
  • 1
  • 4
  • On top of this, in my case, I had to delete the `target/test-classes` folder, clean and rebuild the workspace. Started working after that. – Whyves Jul 10 '15 at 03:12
  • My `target` folder was empty in Eclipse. I refreshed the project and it worked. (Click your project in Eclipse and press F5) – Philip Rego Mar 02 '22 at 20:11
  • If you then get ClassNotFoundException you need to Right Click project | Build Path | Configure Build Path... | Order and Export tab | move "Maven Dependencies" above "JRE System Library" – Philip Rego Mar 02 '22 at 20:35
2

Possibly src/test is not in the Java Build Path.

Solution on Kepler:

Project -> Build Path -> Configure Build Path -> Source -> Add Folder

Then check the box corresponding to test under src

CuongHuyTo
  • 1,333
  • 13
  • 19
1

You might find this is likely caused by using Maven to build (Maven usually builds into the 'target' folder), but Eclipse is using a different build folder for its own build process. Simplest way is to go into the target folder under your Eclipse Project (or Bundle if using OSGi) and delete the conflicting subfolders/class-files from under that directory; for me this is my "target" folder. Then get Eclipse to rebuild, and everything should be fine.

Technically, and alternatively, you could just blow away the entire build/target folder if you wanted to, and let Eclipse rebuild everything.

Shane
  • 11
  • 2
1

In response to the answer provided by Ryan Dawe, I have found out that Default output folder can be set to only one folder, for all the source folders on build path. So if i changed the output folder to target/test-classes, my src/main/java was also outputting classes there. You might have written this response for a different older version of eclipse, but as of Mars.2 release, we can only have one default output folder for all source folders.

The best solution i have found so far for this problem is to just include the target/test-classes as a class folder, by going to Project -> Properties -> Java Build Path -> Libraries -> Add Class folder.

Saad
  • 915
  • 9
  • 19
  • There should be an 'output folder' node when you expand the upper level source folders node. If you still can't see it, try going via the Package Explorer as described at https://stackoverflow.com/questions/6087690/eclipse-maven-junit-tests-not-compiled-when-running-them – Trent Jun 21 '17 at 01:50
1

Maybe you "just" need to create a new Run configuration. Eclipse "remembers" the latest used Run configuration and just repeats it if not told otherwise. To make sure you have a new Run Configuration you can rightclick the test case in the package explorer and choose Run As | Junit Test. Next time you hit play this will be the "remembered" Run configuration etc.

benc_cneb
  • 31
  • 1
0

It seems that your project wasn't recompiled. Either check Menu:Project/Build Automatically or do it manually as Boris Pavlocic commented.

stacker
  • 68,052
  • 28
  • 140
  • 210
0

Add "test" in front of your test classes if not there already the @Test annotation isn't always picked up from Eclipse's Junit Test framework.

Sara N
  • 1
0

Here is how i fixed my problem...

  1. right click project and go to Run As -> Run Configurations...
  2. select JUnit -> [project-name] on the the popup that came up (this configuration of [project-name] was created for me by eclipse but if not there you can right click JUnit -> New and create it)
  3. goto Classpath tab
  4. highlight User Entries and click Advanced... button
  5. on the Advanced Options popup that came up select Add Folders and click OK
  6. on the Folder Selection popup that came up scroll to your project open up target and select test-classes and click OK
  7. repeat steps 4-6 for the [project-name]/target/classes directory and any other directory needed in your classpath (like properties files used in your tests etc.)

Note: this assumes your project's default output folder for tests is target/test-classes, if it is not then adjust accordingly. Also, make sure you have the right JUnit version selected under the JUnit Run Configuration as well and your src/test/java directory is a source folder to your project, etc. as mentioned by others.

user2052618
  • 556
  • 2
  • 7
  • 20
0

It means that you have created a Test class that you haven´t build yet. After a build, for example with "gradle build" the Test class will be found by Eclipse too. In my case I had to make a cleanup before as well.

Laura Liparulo
  • 2,849
  • 26
  • 27
0

This seems to be the same issue as junit not using the newest file

The problem seems to be that Eclipse puts the compiled tests in the wrong folder which can be solved by manually specifying where they should end up.

Community
  • 1
  • 1
Vilhelm
  • 91
  • 1
  • 5