Thursday, February 3, 2011

Extend TestCase and use annotations in JUnit 4

JUnit 3 required that test classes extend, directly or indirectly, the class junit.framework.TestCase, and that each test method begin with the word "test". In contrast, JUnit 4 uses @Test annotations for each test method and does not require the test class to extend junit.framework.TestCase. But what if we have some JUnit 3 and some JUnit 4 tests? JUnit 4 automatically treats classes that extend TestCase as a JUnit 3 test, and therefore ignores the @Test notation. So let's say there is a class like the following.

public class MyTests extends TestCase
{
@Test
public testOne()
{
//do stuff
}

@Test
public anotherTest()
{
//do stuff
}
}

Using JUnit 4, it would appear that both these tests are run, but actually only the first one runs. Why? Because the class extends TestCase, so JUnit 4 will handle this class using a JUnit 3 compatibility mode. This means that the @Test tag is ignored and the method names are examined for the pattern "test*". This can be fixed in two ways. The first is by removing "extends TestCase". This will cause JUnit 4 to run the test in normal mode instead of backwards compat mode.

The second option is to use a class annotation to force the class to be treated as a JUnit 4 test case. It looks like this:

@RunWith(JUnit4.class)
public class MyTests extends TestCase
{
...
}

This forces JUnit 4 to locate tests using annotations instead of using the older method name convention.

The solution using @RunWith was found in the JUnit yahoo group.

2 comments:

Anonymous said...

thank you

Step8P said...

Great solution (@RunWith(JUnit4.class)