You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Writing a Test Case

Test cases are crucial for debugging your code. Test cases can also verify that changes to the codebase do not break existing/debugged/working functionality.

This tutorial shows how to write a JUnit test case using the Netbeans IDE.

Test Case Skeleton

Below is minimal code that can be used as a template for writing org.lcsim test cases. It is not meant as an example, working test case but as an illustration of the essential parts of a JUnit test case. Each section will covered in detail.

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestCaseExample extends TestCase
{
    public void TestCaseExample() throws Exception
    {}

    public static Test suite()
    {
        return new TestSuite(TestCaseExample.class);
    }

    protected void setUp() throws Exception
    {
        // DO SETUP HERE
    }

    public void testIt() throws Exception
    {
        // DO TEST HERE
    }
}

First, three JUnit classes need to be imported.

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

TestCase is the base class for writing tests using the JUnit package.

Test and TestSuite provide classes for the framework to create testable objects from your test case.

All test cases need to extend the TestCase class so that JUnit knows that the class provides a test case.

public class TestCaseExample extends TestCase

The constructor for this class should be public, or JUnit cannot access it.

public void TestCaseExample() throws Exception

The suite() function is necessary boilerplate for JUnit. Replace "TestCaseExample" with the actual name of your class.

Test cases in org.lcsim need to implement this function as follows.

public static Test suite()
{
  return new TestSuite(TestCaseExample.class);
}

If your test case requires setup for all its test functions, implement this function, which will be called before any of the tests are executed.

protected void setUp() throws Exception

Finally, implement individual test cases by writing functions that start with the word "test". JUnit will automatically execute any public function starting with this string.

public void testIt() throws Exception

Writing Test Statements

The easiest way to make a test is throwing an error when a result does not match an expected quantity.

In Java 1.5, the assert statement can be used.

assert( results == expected );

In this case, an exception will be thrown if the two variables are not equal. Both results and expected will be printed if this error occurs.

Alternately, an exception can be thrown directly.

if ( results != expected ) {
  throw new RuntimeException("test failed");
}

If any method or constructor called by your test case throws an exception, the test will fail unless the exception is caught and ignored. In general, test methods should propagate exceptions upward so that failure occurs, unless the exception does not indicate an error. This means that test methods should be declared to throw exceptions.

File and Directory Structure

Maven establishes an organization for tests.

Accessing Test Data using Resources

A Real Test Case Example: HitPositionTest

  • No labels