1. import

import the JUnit classes so you can use them in your code.

import junit.framework.TestCase;
...

2. create your class

your class should be named using the JUnit convention with the name of the class you are testing followed by the word Test.

If you class is named Student, then your test will be named StudentTest

...
public class StudentTest
    extends TestCase {

  // code goes here...
}

3. define your test cases

test cases are defined using methods named testXXX()

typically your method will be named following the method you are testing in your source code. The example to the right tests the setName() method, hence the name testSetName().

you have to test your assumptions/expectations of your code using an assertXXX() statement. These are defined in the JUnit package.

ready to go! Run this code to see the results of testing the student class.

import junit.framework.TestCase;

public class StudentTest
    extends TestCase {

  private Student aStudent;     // fixture to be used for testing

  public void setUp() {
    aStudent = new Student("Joe", "888-2993");
  }

  public void testSetName() {
    aStudent.setName("Manuel");
    assertEquals(aStudent.getName(), "Manuel");
 

  }
}

References and Notes

  1. JUnit.org has more information on how to use the package.
  2. Documentation for the asserts in JUnit http://www.junit.org/apidocs/org/junit/Assert.html