javablogspot

Just another WordPress.com weblog

JUnit 3 & JUnit 4: Oil & water

Posted by damuchinni on February 15, 2009

Version 4 of the popular JUnit test framework has been available for use for quite some time (In fact, it is up to version 4.5). However, many projects have a wealth of JUnit 3-style tests and developers may choose to continue using it. If, on the other hand, you decide to dip your toes in JUnit 4 waters, make it a complete immersion. Don’t try to mix and match.

As you may be aware, couple of the touted “benefits” of JUnit 4 are the obviation of extending the TestCase class and the identification of test and lifecycle methods (setUp and tearDown) using Java 5 annotations. For example, the following is a valid JUnit 4-style test case (minus the necessary imports):

public class BusinessLogicTests {
@Before
public void do_this_before_every_test() {
// set up logic goes here
}

@After
public void clean_up_after_a_test() {
// teardown logic goes here
}

@Test
public void addition() {
assertEquals("Invalid addition", 2, 1+1);
}
}

Observe how

  • The class does not extend TestCase
  • The “setup” and “tearDown” methods are identified by annotations
  • The test method does not require a “test” prefix and is also annotated

I’ll let you be the judge of whether this is an improvement on JUnit 3. What I want to alert you to are the perils of subconciously mixing JUnit-3 and JUnit-4 style programming. Consider the following test case:

public class BusinessLogicTests extends TestCase {
@Before
public void do_this_before_every_test() {
// set up logic goes here
}

@After
public void clean_up_after_a_test() {
// teardown logic goes here
}

@Test
public void testAddition() {
assertEquals("Invalid addition", 2, 1+1);
}

@Test
public void subtraction() {
assertEquals("Invalid subtraction", 1, 1-1);
}
}

This will result in a green-bar even though there is an error in the test of subtraction. Can you guess why?

It was that pesky little extends TestCase that I added from force of habit. The flips the switch to JUnit-3, which knows nothing about annotations, ignores my setup and teardown methods and looks for methods prefixed by test. It completely ignores the method named subtraction!

And of course, conversely, the following is just a plain old Java class that won’t be acknowledged by either JUnit-3 or JUnit-4.

public class BusinessLogicTests {
public void testAddition() {
assertEquals("Invalid addition", 2, 1+1);
}
}

Final word of advice, if you are using Struts Test Case, you have no choice but to write JUnit-3 style tests since the parent class of the framework (MockStrutsTestCase) extends Junit-3’s TestCase

.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: