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

Compare with Current View Page History

« Previous Version 2 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.

public class TestCaseExample extends TestCase

File and Directory Structure

Accessing Test Data using Resources

A Real Test Case Example: HitPositionTest

  • No labels