可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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).
回答2:
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.
回答3:
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
回答4:
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.
回答5:
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.
回答6:
It seems that your project wasn't recompiled. Either check Menu:Project/Build Automatically or do it manually as Boris Pavlocic commented.
回答7:
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.
回答8:
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.
回答9:
Here is how i fixed my problem...
- right click project and go to Run As -> Run Configurations...
- 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)
- goto Classpath tab
- highlight User Entries and click Advanced... button
- on the Advanced Options popup that came up select Add Folders and click OK
- on the Folder Selection popup that came up scroll to your project open up target and select test-classes and click OK
- 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.
回答10:
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.