Java 未发现测试运行程序“JUnit 4”的测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2332832/
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
No tests found with test runner 'JUnit 4'
提问by user281070
My Java test worked well from Eclipse. But now, when I relaunch test from the run menu, I get the following message:
我的 Java 测试在 Eclipse 中运行良好。但是现在,当我从运行菜单重新启动测试时,我收到以下消息:
No tests found with test runner 'JUnit 4'
In the .classpath
file I have all jar
files, and at the end have:
在.classpath
文件中我有所有jar
文件,最后有:
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
How can I resolve this error and get tests running again?
如何解决此错误并重新运行测试?
回答by b.roth
Is your Eclipse project maven based? If so, you may need to update the m2eclipse version.
您的 Eclipse 项目是基于 maven 的吗?如果是这样,您可能需要更新 m2eclipse 版本。
Just a quick note: I have a project in Eclipse which is maven-based, and generated initially using the "new maven project" wizard in Eclipse. I'm using JUnit 4.5 for the unit tests, and could quite happily run the tests from the command line using maven, and individual tests from Eclipse using run as JUnit test.... However, when I tried to run all of the tests in the project by invoking run as JUnit test... on the project root node, Eclipse complained "no tests found with test runner junit 4". Solved by upgrading m2eclipse to the latest stable development build from the m2eclipse update site (specifically, I upgraded from version 0.9.8.200905041414 to version 0.9.9.200907201116 in Eclipse Galileo).
只是一个简短的说明:我在 Eclipse 中有一个基于 maven 的项目,最初是使用 Eclipse 中的“新 maven 项目”向导生成的。我正在使用 JUnit 4.5 进行单元测试,并且可以很高兴地使用 maven 从命令行运行测试,并使用 run as JUnit test 从 Eclipse 中运行单个测试......但是,当我尝试运行所有测试时在项目中,通过调用 run as JUnit test... 在项目根节点上,Eclipse 抱怨“没有使用 test runner junit 4 找到测试”。通过从 m2eclipse 更新站点将 m2eclipse 升级到最新的稳定开发版本来解决(具体来说,我在 Eclipse Galileo 中从版本 0.9.8.200905041414 升级到版本 0.9.9.200907201116)。
From here: http://nuin.blogspot.com/2009/07/m2eclipse-and-junit4-no-tests-found.html
从这里:http: //nuin.blogspot.com/2009/07/m2eclipse-and-junit4-no-tests-found.html
回答by VonC
May be your JUnit launch configuration was for a individual test class, and you somehow changed that config to "run all tests in a source folder, package or project"
可能是您的 JUnit 启动配置是针对单个测试类的,并且您以某种方式将该配置更改为“在源文件夹、包或项目中运行所有测试”
But that could trigger the "No tests found with test runner 'JUnit 4'" error message.
但这可能会触发“未找到测试运行程序‘JUnit 4’的测试”错误消息。
Or you did a modification in your test class, removing the @Test
annotation.
See this wiki page.
或者您在测试类中进行了修改,删除了@Test
注释。
请参阅此维基页面。
回答by JamesG
When we get these errors it seems like Eclipse is just confused. Restart Eclipse, refresh the project, clean it, let Eclipse rebuild it, and try again. Most times that works like a charm.
当我们收到这些错误时,似乎 Eclipse 只是感到困惑。重新启动Eclipse,刷新项目,清理它,让Eclipse重建它,然后再试一次。大多数时候,这就像一个魅力。
回答by Germán
this just happened to me. Rebuilding or restarting Eclipse didn't help.
这只是发生在我身上。重建或重新启动 Eclipse 没有帮助。
I solved it by renaming one of the test methods to start with "test..." (JUnit3 style) and then alltests are found. I renamed it back to what it was previously, and it still works.
我通过将其中一种测试方法重命名为以“test...”(JUnit3 样式)开头,然后找到所有测试来解决它。我把它改回了以前的样子,它仍然有效。
回答by Footix29
I tried the solution from Germán. It worked for all the method from my class but i have a lot of classes in my project.
我尝试了 Germán 的解决方案。它适用于我课程中的所有方法,但我的项目中有很多课程。
So I tried removing from build path and then re-adding it. It worked perfectly.
所以我尝试从构建路径中删除,然后重新添加它。它工作得很好。
Hope it helps.
希望能帮助到你。
回答by Anna
This happened to me too. I found that in Eclipse I didn't make a new Java Class file and so that's why it wasn't compiling. Try copying your code into a java class file if it's not already there and then compile.
这也发生在我身上。我发现在 Eclipse 中我没有创建一个新的 Java Class 文件,所以这就是它没有编译的原因。如果您的代码不存在,请尝试将其复制到 Java 类文件中,然后进行编译。
回答by toomasr
I was facing the same problem and I debugged it to bad examples on the web and internals of junit. Basically don't make your class extend TestCase as some examples show for Junit 4.x. Use some naming convention Testor if you want to have an annotation you can use @RunWith(JUnit4.class).
我遇到了同样的问题,我将它调试为 web 和 junit 内部的不良示例。基本上不要让你的类扩展 TestCase,因为一些例子展示了 Junit 4.x。使用一些命名约定Test或者如果你想要一个注释,你可以使用 @RunWith(JUnit4.class)。
If you need access to assert methods extend Assert or use static imports.
如果您需要访问断言方法,请扩展断言或使用静态导入。
If your class extends TestCase then even if you use Junit 4 Runner it will be run as 3. This is because in the initialization code there is detection:
如果您的类扩展了 TestCase 那么即使您使用 Junit 4 Runner 它也会作为 3 运行。这是因为在初始化代码中有检测:
See JUnit3Builder and the lines:
请参阅 JUnit3Builder 和以下几行:
boolean isPre4Test(Class<?> testClass) {
return junit.framework.TestCase.class.isAssignableFrom(testClass);
}
This returns true and the test for junit4 compatibility won't be tried.
这将返回 true,并且不会尝试 junit4 兼容性测试。
回答by nephilim
Check if your test class extends "TestCase". if so, remove that clause. Your class does not need to extend from "TestCase" class. It's most of the cases I've met.
检查您的测试类是否扩展了“TestCase”。如果是这样,请删除该条款。您的类不需要从“TestCase”类扩展。这是我遇到的大多数情况。
public class MyTestCase extends TestCase{
@Test
public void checkSomething() {
//...
}
}
//Result> AssertionFailedError: No test Found in MyTestCase
Following TestCase should be fine.
遵循 TestCase 应该没问题。
public class MyTestCase {
@Test
public void checkSomething() {
//...
}
}
//Works fine
回答by Dima
In context menu of your 'test' directory choose 'Build path' -> 'Use as a source folder'. Eclipse should see your unitTests.java files as a source files. Warning 'No JUnit tests found' occures because there is no unitTests.class files in your 'build' directory
在“测试”目录的上下文菜单中,选择“构建路径”->“用作源文件夹”。Eclipse 应该将您的 unitTests.java 文件视为源文件。警告“未找到 JUnit 测试”,因为“构建”目录中没有 unitTests.class 文件
回答by Martin C.
I found out that Eclipse seems to only perform JUnit 3 style tests if your test-class extends from TestCase
. If you remove the inheritance, the annotations worked for me.
我发现如果您的测试类从TestCase
. 如果您删除继承,注释对我有用。
Beware that you need to statically import all the required assert*
methods like import static org.junit.Assert.*
.
请注意,您需要静态导入所有必需的assert*
方法,例如import static org.junit.Assert.*
.