Pages

22 May 2011

Creating JUnit Test Case, Rapidly in Eclipse

Implementing JUnit test case for java class is much more easier than ever with Eclipse IDE. The framework of JUnit test case is generated by following some simple menus.

I used the case of Java Puzzlers' second chapter's first puzzle for demonstration. I implement an Oddity class has three methods (isOdd, isOdd2, isOdd3) each of which takes an integer as parameter and returns true if the integer parameter is odd. This puzzle is examining a trap can fail any java developer from novice to professional, since method is so simple and generally, anybody doesn't focus on simple things while dealing with problems.

For creating a JUnit Test Case for Oddity class methods, first right click on Odditiy class on package view select New > "JUnit Test Case" as shown below.


On the new menu, the test case name is automatically filled with appending original class name and "Test" word. Only change the "source folder" (generally test) to where test cases located and then press "Next".


 Select all methods of Oddity class (isOdd, isOdd2, isOdd3). 


This menu adds the JUnit library to build path for developer if "Perform the following action:" is selected.


After all, OddityTest class is created under the same package with Oddity (chapter2.puzzle1) in test source folder.


This process decreases the work load for creating JUnit test case for a class. In addition to that, it clarifies which method is being tested with a standard naming that derives from the original method name.

No comments:

Post a Comment

Thx for reading! Comments are appreciated...