Search code examples
eclipseconfigurationjunit4junit3

Disable import of JUnit 3 classes


I want to develop JUnit 4 tests only. When writing Unit Tests, Eclipse often imports classes from junit.framework, which is JUnit 3.

This has lead to various problems, e.g. when expecting an Exception, it simply doesn't catch it if it's in the wrong package like this:

import static org.junit.Assert.*;
import junit.framework.ComparisonFailure;
[...]
try 
{
     assertEquals(0, 1);
}
catch(ComparisonFailure cfe)
{
}

Strange enough, if I Ctrl+Click on ComparisonFailure, it says

Source not found

The JAR of this class belongs to container 'JUnit 4' [...]

Perhaps helpful environment information:

  • I don't have JUnit 3 in my build path.
  • Eclipse Luna 4.4.1

How can I stop Eclipse from importing JUnit 3 classes?

I have read Why is Eclipse using JUnit 3 when I have junit-4.3.1.jar in my build path?, but it's rather old and probably does not apply to Luna any more. Also, my problem is not in running the test, it's in implementing the test.


Solution

  • Actually, JUnit 4 depends on some of the classes that were developed originally within JUnit 3 or reside in packages junit.*. One of such class is ComparisonFailure. If you look at latest JUnit 4.12 you will see that these packages are still there.

    However, sources jar do contain java files for these classes. Perhaps your library that contains JUnit (do you use Eclipse JUnit library?) lacks source files for these? Where does your dependency (junit.jar) come from?