Tuesday, May 11, 2010

JUnit to test Java Programs

JUnit is a framework for developing unit tests for Java classes. JUnit provides a base class called TestCase that can be extended to create a series of tests for the class you are creating, an assertion library used for evaluating the results of individual tests, and several applications that run the tests you create.

You need to create a new separate class XText if our original programs class name is X.

There are three things you must do as you set up this class in order to run your tests:

  1. Import junit.framework.*.
  2. Extend TestCase.
  3. Provide a constructor with a single String parameter. This constructor should call the TestCase constructor with the parameter.

Here is a minimal class that demonstrates the structure of classes derived from TestCase:

import junit.framework.*;  
public class XTest extends TestCase {  
 public XTest(String name)  { 
    super(name); 
  }    
 public void Y()  {   .........  } 
} 

No comments:

Post a Comment