10 Differences Between JUnit 3.x and JUnit 4.x and why you should move to JUnit 4.x platform.

There is a great change from JUnit 3.x to JUnit 4.x. The JUnit Annotations in 4.x make unit testing pretty simpler and quicker now. In this article, I will try to cover 10 different JUnit features to show why you or your company should migrate from using JUnit 3.x to JUnit 4.x.

1. No more setUp() and tearDown() methods.

The methods setUp() and tearDown() methods can now be replaced with annotations @Before and @After and you can name your method as you would like. In Junit 3.x, if you wanted to do initialization and clean up before and after each test run, you had to write methods with specific names as below:

Intialization:

public void setUp () {
   //Your Code Here

}

CleanUp:

public void tearDown() {
   //Your Code Here
}


You no longer need to follow the naming convention in Junit 4.x, as long as you use annotation tags @before to do initialization and @after to do clean up.

Intialization:

@before
public void myOwnInitializationMethod () {
   //Your Code Here

}

CleanUp:

@after
public void myOwnCleanupMethod() {
   //Your Code Here
}

2. You don’t need to name your method to start with the word test.

In Junit 3.x, the jUnit methods had to be specifically named. They needed to begin with the word test in order for JUnit to run that as a test case.

Junit 3.x:

public void testMyMethod() {
   //Your Code Here
}


JUnit 4.x:

@Test
public void myMethod() {
  //Your Code Here
}

3. You don’t need write your test class to extend junit.framework.TestCase

jUnit 3.x:

public class MyTestClass extends TestCase {
  // Your Methods Here

}


jUnit 4.x:

@TestClass
public class MyTestClass {
   //Your Methods Here
}

4. JUnit 4 adds ability to do one time setup and one time tear down.

If you wanted to do one time setup (intialization at the beginning of the class load) and one time tear down (cleaning up after all your test cases have been run), you can now do that with @BeforeClass and @AfterClass annotations.

@BeforeClass
public void mySomeMethod() {
//This method is run one time before any of your test cases are run.
}
@AfterClass
public void mySomeClenaUpMethod {
//This method is run one time after all your test cases have been run
}

5. JUnit 4 allows static import of the assert classes.

6. You can ingnore a test case with @Ignore annotation

@Ignore
public void doNotRunThisTest() {

}

<!--INFOLINKS_ON-->
<h2>7. Enforcing Timeout</h2>
You can time limit your test cases by providing timeout value. If the test case does not complete within that time, JUnit reports this as a failure.
<!--INFOLINKS_OFF-->

@Test(timeout=50)
public void runThisTestWithin50ms() {
}

8. JUnit Test Suite

Running Junits as a suite is now simpler. See the tutorial on running JUnit 4.0 test suite at:
http://sanjaal.com/java/tag/junit-test-suite-tutorial/

9. Expecting Exception:

Junit 4.x allows you to expect exceptions in your unit test. So, if you are expecting NullpointerException, here is what your code would look like:

@Test(expected=NullPointerException.class)
public void testMyMethodForNullPointer() {
 //Your piece of code that throws null pointer exception
}

10. Parameterized Testing:

With JUnit 4.x, you can run the same tests with multiple parameters using some annotations. To write parameterized testing, you can put class level annotation @RunWith(Parameterized.class) to tell JUnit to run parameterized testing. Then inside your class, you can use annotation @Parameters to tell JUnit a list of collections to use as parameters. The constructor will know how to initialize your parameters.

@RunWith(value=Parameterized.class)
public class ParameterizedTest {

   private String myFirstParam;
   private String mySecondParam;

   @Parameters
   public static Collection paramsValues {
       return Arrays.asList( new Object[][] {
                            { "1", "1" },
                            { "11", "11" },
                            { "12", "12" },
                            { "13", "13" },
                            { "16", "16" },
                            });
   }

   public ParameterizedTest(String paramOne, String paramTwo) {
       this.myFirstParam = paramOne;
       this.mySecondParam = paramTwo;
   }

   @Test
   public void testParams() {
       Assert.assertEquals(myFirstParam, mySecondParam);
   }
}