JUnit example - a JUnit JBuilder template

Here is a JUnit template I use with JBuilder:

package PACKAGE;

import junit.framework.*;

public class CLASSNAME extends TestCase
{

  /**
   * Set up work to be done before test cases.
   */
  protected void setUp()
  {
  }

  /**
   * do a tear down after each test
   */
  protected void tearDown()
  {
  }


  public void testPOBox123()
  {
    String address = "P.O. Box 123";
    String numericAddress = AddressTool.convertAddress(address);
    String expectedResult = "123";
    assert( "\ntestNull, EXPECTED: " + expectedResult + ", GOT: " + numericAddress, numericAddress.equals(expectedResult) );
  }


  public void testBlank()
  {
    String address = "";
    String numericAddress = AddressTool.convertAddress(address);
    String expectedResult = "";
    assert( "\ntestNull, EXPECTED: " + expectedResult + ", GOT: " + numericAddress, numericAddress.equals(expectedResult) );
  }


  public void testNull()
  {
    String address = null;
    String numericAddress = AddressTool.convertAddress(address);
    String expectedResult = "";
    assert( "\ntestNull, EXPECTED: " + expectedResult + ", GOT: " + numericAddress, numericAddress.equals(expectedResult) );
  }


  public static void main(String args[])
  {
    junit.textui.TestRunner.run(CLASSNAME.class);
  }

  public CLASSNAME(String name)
  {
    super(name);
  }

  /**
   * add one line here for each test in the suite
   */
  public static Test suite()
  {
    TestSuite suite = new TestSuite();

    // run tests manually
    //suite.addTest( new CLASSNAME("testBlank") );
    //suite.addTest( new CLASSNAME("testNull") );
    //return suite;

    // or, run tests dynamically
    return new TestSuite(CLASSNAME.class);
  }

}

Some of this stuff isn't needed every time, but I keep it in the template as a reminder. Inside of JBuilder I name this template junitTestCase.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.