Saturday, December 31, 2011

Creating a Junit Test Class

The Procedure to create a JUnit Test Class is as follows:
1. There are many ways to create a JUnit Test Case Class.
  1. Select File > New > JUnit Test Case
  2. Select the arrow of the  New icon (shown at the end of the post)  in the upper left of the toolbar. Select JUnit Test Case,
  3. Right click on a package in the Package Explorer view in the Java Perspective, and select JUnit Test Case, or
  4. Click on the arrow of the  New Java Class icon (shown at the end of the post)  in the toolbar. Select JUnit Test Case.
  5. You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestCase as the super class of the test class you are creating.
  6. Right click on an application class that you want to create a test class for, and select New > JUnit Test Case.
    2. In the wizard for creating a new JUnit test case classes
    • The source folder specifies where you want to create the test case. Based on the project hierarchy presented in previous post, we want to create the new test case in the tests/ source folder.(in our case 'ExampleJunit/tests' )
    • Next, specify the package you want your test case to belong to. If you are associating your test case with an application class, then you will want both of these classes to share the same package so you can take advantage of package level visibility when testing your application class.(in our case 'test' )
    • Specify the test case name. If you are associating your test case with an application class the convention is that the test case name is the application class name followed by Test.(in our case 'SampleTest')
    • JUnit has several standard methods that are useful for testing. setUp() and tearDown() methods are run before and after each test method.
    • If you are associating your test case with an application class, then Browse for the application class in the Class under test field.
    3. If you selected a Class under test you can click the Next button to select which methods you want to write test cases for. The method signatures will be created for you. Click Finish and the new test case class will open in the editor.

      4. Below is a test case template from the JUnit 3 .This test class demonstrates the basic functionality of the setUp() andtearDown() methods, and gives example test cases. The testForException() method demonstrates how to test that an exception is properly thrown.  

      Note:All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is protected, the test class must be in the same package. 
      1. import junit.framework.TestCase;
      2. public class SampleTest extends TestCase {
      3.     private java.util.List emptyList;
      4.     /**
      5.     * Sets up the test fixture.
      6.     * (Called before every test case method.)
      7.     */
      8.     protected void setUp() {
      9.       emptyList = new java.util.ArrayList();
      10.     }
      11.     /**
      12.     * Tears down the test fixture.
      13.     * (Called after every test case method.)
      14.     */
      15.     protected void tearDown() {
      16.       emptyList = null;
      17.     }
      18.     public void testSomeBehavior() {
      19.       assertEquals("Empty list should have 0 elements"0, emptyList.size());
      20.     }
      21.     public void testForException() {
      22.       try {
      23.            Object o = emptyList.get(0);
      24.            fail("Should raise an IndexOutOfBoundsException");
      25.       }
      26.       catch (IndexOutOfBoundsException success) {
      27.       }
      28.     }
      29. }
      5. Below is a test case template based on the examples presented in the Junit 4.This test class demonstrates the basic functionality of the setUp() and tearDown() methods, and gives example test cases.The testForException() method demonstrates how to test that an exception is properly thrown.

      Note: All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is protected, the test class must be in the same package. 
      1. import org.junit.*;
      2. import static org.junit.Assert.*;
      3. public class SampleTest {
      4. private java.util.List emptyList;
      5.     /**
      6.      * Sets up the test fixture.
      7.      * (Called before every test case method.)
      8.      */
      9.     @Before
      10.     public void setUp() {
      11.         emptyList = new java.util.ArrayList();
      12.     }
      13.     /**
      14.      * Tears down the test fixture.
      15.      * (Called after every test case method.)
      16.      */
      17.     @After
      18.     public void tearDown() {
      19.         emptyList = null;
      20.     }
      21.     @Test
      22.     public void testSomeBehavior() {
      23.         assertEquals("Empty list should have 0 elements"0, emptyList.size());
      24.     }
      25.     @Test(expected=IndexOutOfBoundsException.class)
      26.     public void testForException() {
      27.         Object o = emptyList.get(0);
      28.     }
      29. }
      Icons Used:
          
       
         New

      New Java Class

      No comments:

      Post a Comment