JUnit 无法在 Eclipse 中找到测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2998528/
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
JUnit unable to find tests in Eclipse
提问by mikera
I have a strange issue with JUnit 4 tests in Eclipse 3.5 that I couldn't solve - any hints gratefully received!
我在 Eclipse 3.5 中的 JUnit 4 测试有一个奇怪的问题,我无法解决 - 感谢收到任何提示!
Initially: I had a test suite working properly, with 100+ tests all configured with JUnit 4 annotations. I'd run these typically by right clicking on my source folder and selecting "Run as JUnit test". All worked perfectly.
最初:我有一个测试套件正常工作,有 100 多个测试全部配置了 JUnit 4 注释。我通常通过右键单击我的源文件夹并选择“作为 JUnit 测试运行”来运行这些。一切都完美无缺。
Now: When I try to run the test messages all I get is an error "No tests found with test runner 'JUnit 4'".
现在:当我尝试运行测试消息时,我得到的只是一个错误“未找到测试运行程序‘JUnit 4’的测试”。
Any idea what is happening? I simply can't work out what could have changed to make this fail.
知道发生了什么吗?我根本无法弄清楚什么可能会导致失败。
My guess is that it is some configuration issue based on the build path or class path?
我的猜测是基于构建路径或类路径的一些配置问题?
采纳答案by mikera
I never found out the real cause of this issue but an upgrade to Eclipse 3.6 finally solved it.
我从来没有发现这个问题的真正原因,但升级到 Eclipse 3.6 终于解决了它。
回答by Akku
Had this issue too, doing a "Project --> Clean" and then trying to run the unit tests as usual already did it!
也有这个问题,做一个“项目->清理”,然后像往常一样尝试运行单元测试已经做到了!
回答by John Patterson
I solved a problem like this differently. None of these solutions worked for me. My project is a Google Web App project, a Maven Project, and a JREbel project... so any of these could have been messing with my setup.
我以不同的方式解决了这样的问题。这些解决方案都不适合我。我的项目是一个 Google Web App 项目、一个 Maven 项目和一个 JREbel 项目......所以这些项目中的任何一个都可能会干扰我的设置。
I also found that I could not run any classes with a main() method using Run As -> Java Application. That resulted in a ClassNotFoundException.
我还发现我无法使用 Run As -> Java Application 使用 main() 方法运行任何类。这导致了 ClassNotFoundException。
My solution was to create a new Web Application Project and copy in all the source and set it up again. Now both Java Application and JUnit Test work as expected
我的解决方案是创建一个新的 Web 应用程序项目并复制所有源代码并重新设置。现在 Java 应用程序和 JUnit 测试都按预期工作
回答by Pratap Dessai
funniest answer for this question.
这个问题最有趣的答案。
Change the test name to start with "test"
更改测试名称以“test”开头
For example,
例如,
@Test public void checkForTwoStringTest(){....}
@Test public void checkForTwoStringTest(){....}
Change to
改成
@Test public void testCheckForTwoString
@Test public void testCheckForTwoString
This did work like a miracle.... :-)
这确实像奇迹一样有效...... :-)
回答by Nick Vallely
My first couple of thoughts (without seeing some example test code):
我的前几个想法(没有看到一些示例测试代码):
No tests found with test runner 'JUnit 4'
A lot of suggestions state to restart Eclipse and clean project. In my experience, sometimes eclipse gets stuck in a mode where it thinks I am trying to do a hybrid run between JUnit 3 and 4, so naming the '@Test' method with the old verbiage of naming it 'test...' sometimes works.
许多建议状态重新启动 Eclipse 和清理项目。根据我的经验,有时 eclipse 会陷入一种模式,它认为我正在尝试在 JUnit 3 和 4 之间进行混合运行,因此有时将“@Test”方法命名为“test...”的旧说法作品。
Also if you have any other plugins, i.e. m2eclipse, or other that you have recently added, this could effect your situation as well. If anything you have recently added uses its own version of JUnit, this could cause issues.
此外,如果您有任何其他插件,即 m2eclipse 或您最近添加的其他插件,这也会影响您的情况。如果您最近添加的任何内容使用其自己的 JUnit 版本,这可能会导致问题。
Some sample test code, might help with further investigation.
一些示例测试代码,可能有助于进一步调查。