Unit test class runs old version in eclipse

2020-04-05 02:46发布

问题:

I have a JUnit test class in my project which is updated incrementally - I add tests every few weeks and sometimes modify the tests' code.

Surprisingly, when I run the test class using eclipse JUnit Runner 4, it runs my old code (before the update) and not the new one. I can change the code, add or remove tests but it still runs the old code.

I tried to isolate the problem and run a single test that I've just written and got the infamous "Unrooted tests" error without any stack trace to give me a clue what the problem is.

I did some research and according to a few other threads here, many people encountered this problem with regards to JUnit 3 \ JUnit 4 compatibility, but this is not the case here - I annotate all my tests with @Test and I do NOT extends the TestCase class.

Cleaning / Building all eclipse projects doesn't help. However, this problem does work itself around when I mvn clean install my project, but this takes too much time. Also, renaming the class (Alt+Shift+R in eclipse) gets the new code to run immediately, but renaming it back to its original (and valid) name, gets the old code to run again (WTF?)

Help would be appreciated, 10x

回答1:

I managed to solve the problem on my own (inspired by a comment contributed by Harlard). After examining the target directory of my project, I noticed that the test-classes dir inside it doesn't include the binaries of my test. I then noticed that I misplaced the classes within src/test/java and put them in a package structure that doesn't conform to the package structure of my project, they were direct sub-directories of src/test/java. Therefore, eclipse didn't put them in the correct place and the only way to generate binaries for them was by executing a maven build. After refactoring all my test classes to the correct package structure, everything worked perfectly.



回答2:

Another solution would be manually deleting the "target" folder of the project and rebuilding it. This should solve all problems of this kind.



回答3:

I had the same issue. It was due to a defective .classpath. Removing this .classpath and re-importing the project, generates a correct .classpath and fixed the environment.