Eclipse 看不到我的新 junit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2586457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:35:58  来源:igfitidea点击:

Eclipse doesn't see my new junit test

eclipsejunit

提问by morgancodes

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.

我正在使用 eclipse 在单个 junit(4) 测试类中运行测试。课堂上的测试都运行得很好。然后我添加一个额外的测试,并通过再次在 ecplise 中运行的测试运行该类。只运行旧的测试。eclipse 看不到新的测试。没有错误什么的,就好像eclipse在看旧版本的测试一样。

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.

如果我使用 maven 运行测试,一切正常。此外,在 maven 中运行测试后,eclipse 可以正确查看并运行新测试。

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

任何想法发生了什么?任何想法如何让 ecplipse 的测试运行程序查看我的新测试用例?

回答by Ryan Dawe

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-classesdirectory in your Maven structure
  • 转到Project -> Properties -> Java Build Path
    对于源文件夹src/test/java,输出文件夹设置为“默认输出文件夹”
  • 将此设置为target/test-classesMaven 结构中的典型 Maven目录

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

在此之后,Maven 和 Eclipse 同步(而不是 Eclipse 愉快地运行旧版本的测试,从最后一次 Maven 编译开始)。

回答by CuongHuyTo

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

可能 src/test 不在 Java 构建路径中。

Solution on Kepler:

开普勒解决方案:

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

项目 -> 构建路径 -> 配置构建路径 -> 源 -> 添加文件夹

Then check the box corresponding to testunder src

然后勾选srctest对应的框

回答by Shane

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.

您可能会发现这很可能是由于使用 Maven 进行构建(Maven 通常构建到“目标”文件夹中)造成的,但 Eclipse 正在使用不同的构建文件夹进行其自己的构建过程。最简单的方法是进入 Eclipse 项目(或 Bundle,如果使用 OSGi)下的目标文件夹,并从该目录下删除冲突的子文件夹/类文件;对我来说,这是我的“目标”文件夹。然后让Eclipse重建,一切都应该没问题。

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

从技术上讲,或者,如果您愿意,您可以删除整个 build/target 文件夹,然后让 Eclipse 重建所有内容。

回答by Saad

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.

针对 Ryan Dawe 提供的答案,我发现对于构建路径上的所有源文件夹,默认输出文件夹只能设置为一个文件夹。因此,如果我将输出文件夹更改为 target/test-classes,我的 src/main/java 也在那里输出类。您可能已经为不同的旧版本 eclipse 编写了此响应,但是从 Mars.2 版本开始,我们只能为所有源文件夹使用一个默认输出文件夹。

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.

到目前为止,我找到的最佳解决方案是将目标/测试类作为类文件夹包含,方法是转到 Project -> Properties -> Java Build Path -> Libraries -> Add Class 文件夹。

回答by benc_cneb

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.

也许您“只是”需要创建一个新的运行配置。Eclipse 会“记住”最新使用的运行配置,如果没有另外说明,只会重复它。为了确保你有一个新的运行配置,你可以在包浏览器中右键单击测试用例并选择 Run As | Junit 测试。下次您点击播放时,这将是“记住的”运行配置等。

回答by Vilhelm

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

这似乎与junit 不使用最新文件的问题相同

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.

问题似乎是 Eclipse 将编译的测试放在错误的文件夹中,这可以通过手动指定它们应该在哪里结束来解决。

回答by stacker

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

您的项目似乎没有重新编译。检查 Menu:Project/Build Automatically 或按照 Boris Pavlocic 的评论手动执行。

回答by Sara N

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.

如果没有的话,在你的测试类前面添加“test”,@Test 注释并不总是从 Eclipse 的 Junit 测试框架中提取的。

回答by user2052618

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 -> Newand create it)
  3. goto Classpathtab
  4. highlight User Entriesand click Advanced...button
  5. on the Advanced Optionspopup that came up select Add Foldersand click OK
  6. on the Folder Selectionpopup that came up scroll to your project open up targetand select test-classesand 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.)
  1. 右键单击项目并转到运行方式-> 运行配置...
  2. 在出现的弹出窗口中选择JUnit -> [project-name]([project-name] 的这个配置是由 eclipse 为我创建的,但如果没有,你可以右键单击JUnit -> 新建并创建它)
  3. 转到类路径选项卡
  4. 突出显示用户条目并单击高级...按钮
  5. 在出现的“高级选项”弹出窗口中选择“添加文件夹”,然后单击“确定”
  6. 在出现的文件夹选择弹出窗口中滚动到您的项目打开目标并选择测试类并单击确定
  7. 对 [project-name]/target/classes 目录和类路径中所需的任何其他目录(如测试中使用的属性文件等)重复步骤 4-6

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.

注意:这假设您的项目的默认测试输出文件夹是目标/测试类,如果不是,则相应地进行调整。此外,请确保您在 JUnit 运行配置下也选择了正确的 JUnit 版本,并且您的 src/test/java 目录是其他人提到的项目等的源文件夹。

回答by Laura Liparulo

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.

这意味着您已经创建了一个尚未构建的测试类。在构建之后,例如使用“gradle build”,Eclipse 也会找到 Test 类。就我而言,我之前也必须进行清理。