Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


import static junit.framework.Assert.assertEquals;

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)

thankyou! I did exactly the same thing :)

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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.

If you do not see it in your project dependencies then one of the libraries might be using 3.8. If you cannot remove the library from there then try using the @RunWith(JUnit4.class) annotation.

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.

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.

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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

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.

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

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


import static junit.framework.Assert.assertEquals;

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)

thankyou! I did exactly the same thing :)

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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 same error occurred to me when running my test class in eclipse but my solution was slightly different.

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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.

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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

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.

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

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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 same error occurred to me when running my test class in eclipse but my solution was slightly different.

Note
Rectangle 27 0

unit testing JUnit java.lang.NoSuchMethodError: junit.framework.ComparisonFailure.getExpected()LjavalangString?


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.

If you do not see it in your project dependencies then one of the libraries might be using 3.8. If you cannot remove the library from there then try using the @RunWith(JUnit4.class) annotation.

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.

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.

Note