开发者

JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()Ljava/lang/String

开发者 https://www.devze.com 2023-01-22 10:29 出处:网络
I am getting the following exception from a test case that ran successfully before but now it throws this exception:

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$4.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:1开发者_开发百科18)
    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.


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.

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.

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.


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)

Check both your imports and your static imports - the culprit for me was import static junit.framework.Assert.assertEquals;


The same error occurred to me when running my test class in eclipse but my solution was slightly different.

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.

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


The problem in my case was junit-4.5.jar added directly as a library to the project.

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.

That one takes JUnit from plugin and has a reference similar to this:

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


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.


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.

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

https://www.gotraveltech.com/confluence/display/COOK/My+Eclipse Scroll down to "How do I run individual JUnit 4.x.x tests in MyEclipse?"


Checking for dependencies and imported libraries versions 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:

Exception in thread "main" java.lang.NoSuchMethodError: ...

0

精彩评论

暂无评论...
验证码 换一张
取 消