JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()Ljava/lang/String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4019016/
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 java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()Ljava/lang/String
提问by TheWolf
I am getting the following exception from a test case that ran successfully before but now it throws this exception:
我从之前成功运行的测试用例中收到以下异常,但现在它抛出此异常:
java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()Ljava/lang/String;
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestListener.testFailure(JUnit4TestListener.java:63)
at org.junit.runner.notification.RunNotifier.notifyListener(RunNotifier.java:100)
at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:41)
at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:97)
at org.junit.internal.runners.JUnit38ClassRunner$OldTestClassAdaptingListener.addError(JUnit38ClassRunner.java:41)
at org.junit.internal.runners.JUnit38ClassRunner$OldTestClassAdaptingListener.addFailure(JUnit38ClassRunner.java:64)
at junit.framework.TestResult.addFailure(TestResult.java:46)
at junit.framework.TestResult.runProtected(TestResult.java:127)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Anyone know what is causing this? It seems like an internal issue with the JUnit runner.
有谁知道是什么原因造成的?这似乎是 JUnit 运行程序的内部问题。
采纳答案by skaffman
The getExpected()
method on junit.framework.ComparisonFailure
was only added in JUnit 3.8.2 (remember, junit.framework
package is from JUnit 3.8, whereas JUnit 4 uses org.junit
). The method wasn't there in 3.8.1, which is the most common version of 3.x still out there.
的getExpected()
上方法junit.framework.ComparisonFailure
仅在JUnit的3.8.2中加入(记住,junit.framework
包是从JUnit 3.8中,而JUnit 4层中的用途org.junit
)。该方法在 3.8.1 中不存在,这是仍然存在的最常见的 3.x 版本。
I think the method was added for easier migration to JUnit 4 tooling, and occasionally this exception pops up on older code bases that use JUnit 3.8. The Eclipse JUnit 4 test runner would appear to switch back to calling the junit.framework.*
code when running JUnit 3.8 tests.
我认为添加该方法是为了更轻松地迁移到 JUnit 4 工具,并且偶尔会在使用 JUnit 3.8 的旧代码库中弹出此异常。junit.framework.*
在运行 JUnit 3.8 测试时,Eclipse JUnit 4 测试运行器似乎会切换回调用代码。
SO I'm guessing you still have JUnit 3.8.1 lurking about on your classpath, and it's clashing with the Eclipse test runner. Either get rid of that JAR, or "upgrade" it to 3.8.2.
所以我猜你的类路径上仍然潜伏着 JUnit 3.8.1,它与 Eclipse 测试运行器发生冲突。要么摆脱那个 JAR,要么将它“升级”到 3.8.2。
回答by mtpettyp
This can also occur if you're using JUnit 4 but import the Assert
class from the old junit.framework
package (rather than the new org.junit
package)
如果您使用 JUnit 4 但从Assert
旧junit.framework
包(而不是新org.junit
包)导入类,也会发生这种情况
Check both your imports and your static imports - the culprit for me was import static junit.framework.Assert.assertEquals;
检查您的导入和静态导入 - 我的罪魁祸首是 import static junit.framework.Assert.assertEquals;
回答by dulon
The same error occurred to me when running my test class in eclipse but my solution was slightly different.
在 eclipse 中运行我的测试类时发生了同样的错误,但我的解决方案略有不同。
My setup was that I had junit-4.10 jar in the classpath but my test class was using junit 3 and the exception only occurred if the assertEquals method failed.
我的设置是我在类路径中有 junit-4.10 jar,但我的测试类使用的是 junit 3,并且只有在 assertEquals 方法失败时才会发生异常。
The problem was that eclipse was still using the junit 4 runner so I changed it by editing the configuration (selecting the "Run Configuration" in eclipse, then selecting the test class and then changing the "Test Runner" in the dropdown from "JUnit 4" to "JUnit 3").
问题是 eclipse 仍在使用 junit 4 runner 所以我通过编辑配置来更改它(在 eclipse 中选择“运行配置”,然后选择测试类,然后从“JUnit 4”的下拉列表中更改“测试运行器” ”到“JUnit 3”)。
回答by Ryan
I also had the problem, but it was because I was using JUnit 4.1. I switched to 4.4 and the problem went away.
我也遇到了这个问题,但那是因为我使用的是 JUnit 4.1。我切换到 4.4,问题就消失了。
回答by user2668478
I only had the issue after I upgraded to the Kepler version of Eclipse. The test would run fine if I ran "All" tests. But when I ran a single method test it would fail with this error.
我只是在升级到 Eclipse 的开普勒版本后才遇到这个问题。如果我运行“所有”测试,测试将运行良好。但是当我运行单个方法测试时,它会因此错误而失败。
I found this answer and thought I'd share it here to help someone else in the future. The long and short of it is upgrade to Junit 4.4 or better
我找到了这个答案,并认为我会在这里分享它以帮助将来的其他人。它的长短是升级到 Junit 4.4 或更好
https://www.gotraveltech.com/confluence/display/COOK/My+EclipseScroll down to "How do I run individual JUnit 4.x.x tests in MyEclipse?"
https://www.gotraveltech.com/confluence/display/COOK/My+Eclipse向下滚动到“如何在 MyEclipse 中运行单个 JUnit 4.xx 测试?”
回答by ATrubka
The problem in my case was junit-4.5.jar added directly as a library to the project.
我的问题是 junit-4.5.jar 作为库直接添加到项目中。
My solution was to remove dependency on the junit jar from Eclipse project and rely on the JUnit plugin library. That one is added automatically or can be added manually by clicking Add Library button on the project's library screen.
我的解决方案是从 Eclipse 项目中删除对 junit jar 的依赖,并依赖于 JUnit 插件库。这个是自动添加的,也可以通过单击项目库屏幕上的添加库按钮手动添加。
That one takes JUnit from plugin and has a reference similar to this:
那个从插件中获取 JUnit 并有一个类似于这样的引用:
.../Eclipse/plugins/org.junit_4.12.0.v201504281640
It's also necessary to move the JUnit library on top of the list in Order and Export tab. Checking the checkbox for export is not necessary.
还需要将 JUnit 库移到 Order and Export 选项卡中的列表顶部。无需选中导出复选框。
回答by Zon
Checking for dependencies and imported librariesversions is most productive in this case.
在这种情况下,检查依赖项和导入的库版本的效率最高。
For example, @Test
annotation could be imported from org.junit.Test
and not @org.testng.annotations.Test
. For Android project it could result in an error like:
例如,@Test
注释可以org.junit.Test
从而不是从 导入@org.testng.annotations.Test
。对于 Android 项目,它可能会导致如下错误:
Exception in thread "main" java.lang.NoSuchMethodError: ...
线程“main”中的异常 java.lang.NoSuchMethodError: ...