Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

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

Test Case Skeleton

Tip
titleAutomatic Test Case generation

You can automatically generate a test class with complete test cases in Netbeans. With the class open in the editor, go to the "Tools" menu and select "Create JUnit Tests" or use the shortcut Ctrl+Shift+U. Alternatively, you can right-click on a class in the Files browser and follow the same path, "Tools -> Create JUnit Tests".

 

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 be covered in detail.

No Format
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
    }
}

...

Actual test data is kept at *http://www.lcsim.org/datasamples*Image Removed, which can be browsed. Your test data can also be added here. Contact jeremy@slac.stanford.edu if you want to upload your own test data.

...

For an example test case, look at HitPositionTest in the org.lcsim package. It illustrates a number of the concepts covered in this document.

Adding from Netbeans

To add a test case from Netbeans, expand the project navigation window to the "Test Sources" folder. Right-click on the package where the test case should be added. In this case, the base package area is used, but you should use the actual package of the class being tested.

Image Added

Now type in the name of the class and click "Finish". Follow the template given previously to write the actual test case.