Tutorial: Adding Software Testing to Programming Assignments
Getting Started

JUnit (at http://junit.org/) is a class library for writing unit tests for Java software.

Unit testing is normally practiced by developers, who write tests of individual program units to check them out before releasing them for inclusion in a larger project. It most naturally corresponds to the testing that students might do, since the focus is on testing the features or individual classes or modules. Other kinds of testing include integration testing, which usually focuses on testing interconnected collections of units to make sure they work together when they are integrated into a whole, functional testing, which usually focuses on ensuring an application meets its requirements, and acceptance testing, which is usually performed by a customer to assess whether a development organization has produced an acceptable (custom) product.

JUnit allows one to write software tests in the form of code, so the tests can be easily executed, and can be re-executed as often as you like with near-zero effort. Phrasing tests in the form of code is more useful than running tests by hand, since it makes the process of executing and managing tests very easy to include in automated build processes, and eliminates human error in conducting these actions.

Basic terms:

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

import junit.framework.TestCase;

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");
    }
}

1. import and create your test class

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

import junit.framework.TestCase;

public class StudentTest
    extends TestCase
{
    // code goes here...
}

2. define your fixtures

fixtures are objects that are used in your test cases.

define your fixtures as private instance variables.

initialize your fixture in the setUp() method

setUp() is called before each call to each test case.

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");  // initialize it here
    }
}

3. use fixtures in your test cases

fixture objects created in setUp are new objects in each test case.

the two test cases on the right run succesfully

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

        aStudent.setName("Manuel");
        assertEquals(aStudent.getName(), "Manuel");
    }

    public void testSomethingElse()
    {
        // aStudent .. is new here
        assertEquals(aStudent.getName(), "Joe");
        assertEquals(aStudent.getID(), "888-2993");
    }
}

1. import the student.jar

the LIFT library is included in the student.jar.

import student.*;

2. create your GUI Test Case

create your GUI test class as an extension of the GUITestCase class (part of student.jar)

import student.*;

public class GUITest
    extends GUITestCase
{
    // code goes here...
}

3. create your GUI as a fixture

in your setUp(), create your GUI objects and show them in the testing frame.

showInFrame() is defined in the student.jar

import student.*;

public class GUITest
    extends GUITestCase
{
    private PushCounterPanel aPanel;  // fixture to be used for testing

    public void setUp()
    {
        aPanel = new PushCounterPanel();
        showInFrame(aPanel);    // to test it...
    }
}

4. find the object and interact with it

in your test cases, find an object from your interface using getComponent() and the component filters defined in LIFT.

interact with your interface by clicking or typing or using the mouse, under test control

then check that your application is in the correct state.

public void testPushCounterPanel()
{
    // You can retrieve components by name
    JButton pushMe = getComponent(JButton.class, "button");
    JLabel count = getComponent(JLabel.class, where.nameIs("count"));

    // Interact with your interface, click the button
    click(pushMe);
    click(pushMe);

    // Then, check the state of components
    assertEquals("Pushes: 2", count.getText());
}
  1. JUnit.org has more information on how to use JUnit.

  2. Documentation for the asserts in JUnit: http://www.junit.org/apidocs/org/junit/Assert.html

  3. Testing GUI Programs is a Prezi presentation that gives a good introduction to LIFT and how it is used.

  4. The LIFT website provides more details on LIFT, including links to two sigcse papers, downloads, examples, and discussion forums.