Unit Testing

This is an overview of the unit testing process and tools for the CLforJava project at the College of Charleston.

We are using the NetBeans extension of JUnit for the unit testing procedure for the CLforJava project. JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java. JUnit is Open Source Software, released under the IBM's Common Public License Version 1.0 and hosted on SourceForge.

JUnit module for Netbeans

The JUnit Module makes it possible to generate skeletons for unit tests of individual classes or whole packages, recursively including subpackages and allows to run tests from tested classes or packages. This module brings support for unit and integration testing with JUnit framework and NetBeans extension to JUnit.

This module should be used by developers to produce unit tests for each class, join them into test suites according to packages containing tested classes and to run these tests.Each generated test class contains empty functions for all accessible functions from the tested class, the body of test function has to be filled by the developer to hold some reasonable test code. Generated test classes are compilable and by default prints the name of the function.

Unit Tests Tutorial This tutorial can be found at http://junit.netbeans.org/JUnit_tutorial.html. To proceed with this tutorial, the JUnit Module needs to be installed for developing and running tests.

How to use Junit

How do you write testing code using Junit?

The most important thing is to write tests like

(expectedResult == obtainedResult)

The smallest unit of testing, like the expression above, is a test expression. All that JUnit does is give you flexibility in grouping your test expressions and convenient ways to capture and/or summarize test failures.

JUnit tests do not require human judgment to interpret, and it is easy to run many of them at the same time. When you need to test something, here is what you do:

  1. Create a new test file.
  2. Create test methods that test the original classes methods. They must be predecced by an annotation that marks it as a test case. This annotation looks like @Test. Additionally, the class name should be appended to test (ClassNameMethod). For example, class IntegerImplTest will have test methods such as IntegerImplApply? () and IntegerImplFuncall? ().
  3. Use the junit.framework.Assert to test the expected results to the obtained results.

For Example: Create a new test file.

public class sampleTest{
}

Create test methods that test the original class methods. They start with the testClassName.

@Test
public void testClassnameMethodname() { /* test code */ } 

Use the junit.framework.Assert to test the expected results to the obtained results.

Interface junit.framework.Assert

The Assert interface has a bunch of static convenience methods to help you with individual test expressions. For example, without JUnit :

if (obtainedResult == null || !expectedResult.equals(obtainedResult))
             throw new MyTestException("Bad output for # attempt");

With JUnit, you can code:

 
assertimpl.assertEquals("Bad output for # attempt", obtainedResults, expectedResults);
There are lots of assert* methods to take care of the several common Java types. They take care of checking for nulls, assert() failures (and failure()s, which are the same thing), throw Throwables (probably
RuntimeExceptions
), which is just what you want to happen. If a test expression fails, that test method quits, cleanup is done, and the next test method in the sequence starts (with the error being noted). There are also static failure methods in the Assert class. They do the same thing as a failed assert()-- they throw a Throwable and fail the current test method. Take a look at the API for junit.framework.Assert.

Fixture

What if you have two or more tests that operate on the same or similar sets of objects? Tests need to run against the background of a known set of objects. This set of objects is called a test fixture. When you are writing tests you will often find that you spend more time writing the code to set up the fixture than you do in actually testing values.

To some extent, you can make writing the fixture code easier by paying careful attention to the constructors you write. However, a much bigger savings comes from sharing fixture code. Often, you will be able to use the same fixture for several different tests. Each case will send slightly different messages or parameters to the fixture and will check for different results. When you have a common fixture, here is what you do:

  1. Create a new method in the file named TestFixtures? (Located in test/src/java/lisp)

Suite

How do you run several tests at once? As soon as you have two tests, you'll want to run them together. JUnit provides an object, TestSuite which runs any number of test cases together. The general format is shown below.

package lisp.common.function;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
 * ListFunctionSmokeTest.java
 *
 * Created on January 30, 2010, 5:00 PM
 */
@RunWith(Suite.class)
@Suite.SuiteClasses({
                     lisp.common.function.AppendTest.class,
                     lisp.common.function.AdjustableArrayPTest.class,
                     lisp.common.function.ArefTest.class,
                     ...
})

public class ListFunctionSmokeTest {
    
    /** Creates a new instance of SmokeTest */
    public ListFunctionSmokeTest() {
        System.out.println("Running the ListFunctionSmokeTest");
    }
}

Running Suites

How to get the suites to execute automatically? To do this you must add the suite files to the build xml file, specifically the RunSmokeTestSuite? .xml located in the /BuildEntities

To execute another suite you must append the suite to the middle of the file after the following lines following the same format.

<test name="lisp.common.function.FunctionSmokeTest" todir="${smokeTest.reports.dir}"/>
<test name="lisp.common.function.ListFunctionSmokeTest" todir="${smokeTest.reports.dir}"/>
<test name="lisp.common.function.MathFunctionSmokeTest" todir="${smokeTest.reports.dir}"/>
<test name="lisp.common.function.StringSmokeTest" todir="${smokeTest.reports.dir}"/>

Status

The current status of the testing suites can be viewed on the QAStatus page.

http://clforjava.org/twiki/bin/view/DevEnvironment/QAStatus

More Resources

Get started using Junit in netbeans. Got to the HowTo section.

Here are some resource links that can help:

http://junit.netbeans.org
http://www.junit.org
http://www.junit.org/junit/javadoc/3.8.1/index.htm

-- JerryBoetje - 09 Jul 2003

WebForm
TopicClassification OverView
RelevantComponents UnitTesting
QuickSummary Discussion of use of the open-source JUnit system in the CLforJava project.
Topic revision: r19 - 2010-05-07 - 14:48:07 - StevenDix
 
Home
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback