1

In order to test a Map class I have defined, I have extended MapInterfaceTest from the Google Collections library.

The problem is I want to add more tests to this class, and be able to ignore specific long-running or broken ones with the @Ignore annotation while developing, but because MapInterfaceTest extends TestCase, the tests are run in Junit3 compatibility mode, and the annotations are ignored.

Is there any way to get Junit to respect the annotations on a test class which extends TestCase?

MikeFHay
  • 8,562
  • 4
  • 31
  • 52

1 Answers1

3

Try this annotation at your test class.

@RunWith(JUnit4.class)
public class MyTestCase extends MapInterfaceTest {...}

This should force Junit to use Junit4 not Junit3 in the execution. Maybe that works for your Testcase.

However, JUnit4 will not pick up old-style "test*" test methods, as it relies on @Test annotations. If the class you're extending doesn't have these annotations and you can't add them directly, you can override the tests in order to add the annotations in the subclass, like so:

@Test
@Override
public void testFoo()
{
    super.testFoo();
}
MikeFHay
  • 8,562
  • 4
  • 31
  • 52
Sorontur
  • 500
  • 4
  • 15
  • You're right, this does exactly as you say, annotations are respected but the old-style test* methods in the superclass are ignored. I suppose I could override all the methods like `@Override @Test public void testFoo{super.testFoo();}` and that would solve the problem. – MikeFHay Jun 28 '13 at 10:03
  • ok, great! tell me if that ovrriding did work. that would be intresting. – Sorontur Jun 28 '13 at 10:07
  • It does work, however I decided it was too much work and just split out my new test methods into a different class. I made an edit to your answer, accept it or make a similar edit and I'll accept this answer :) – MikeFHay Jul 02 '13 at 13:17