Previous Next vertical dots separating previous/next from contents/index/pdf

Step 2: Create a Test Class

In this step you will create a test class that will run tests on the control you just created. You can put tests in a separate source folder for better organization and so it's easier to exclude them later during packaging for production. Better yet, you can put the tests in a completely separate project, provided that the project dependencies are correctly configured. But, in this simple case, we will leave the control and test class in the same project.

In this section, you will:

To Create a New Source Folder and Package

Here, you'll create a new source folder to hold the test class.

  1. On the Project Explorer view, right-click MySharedControls and select New> Other.
  2. In the New dialog, open the Java node, select Source Folder, and click Next.
  3. In the Folder name field, enter src-test and click Finish.
  4. On the Project Explorer view, right-click the src-test folder and select New > Package.
  5. In the New Java Package dialog, in the Name field, enter sharedcontrols.test and click Finish.

To Create the Test Class

In this step you will create the class that tests your control.

  1. On the Project Explorer view, right-click the sharedcontrol.test package and select New > Other.
  2. In the New dialog open nodes Java > JUnit, select JUnit Test Case, and click Next.
  3. In the New JUnit Test Case dialog, click the link Click here.


  4. In the Properties for MySharedControls dialog, click OK.


  5. In the New JUnit Test Case dialog, in the Name field, enter EmployeeControlTestCase.
    In the Superclass field, enter org.apache.beehive.controls.test.junit.ControlTestCase.
    (Hint: in the Superclass field enter ControlTestCase and press Ctrl+Space Bar to fill in the remaining package names.)
    In the Class under test field, enter sharedcontrols.EmployeeControl.
    Click Next.

  6. Click Select All and then Finish.


  7. Edit the source code for so it appears as follows. Code to add appears in red. Make sure to delete the line of code fail("Not yet implemented");.
    package sharedcontrols.test;
    import org.apache.beehive.controls.api.bean.Control;
    import org.apache.beehive.controls.test.junit.ControlTestCase;
    public class EmployeeControlTestCase extends ControlTestCase {
        sharedcontrols.EmployeeControl employeeControl;  
         * Test method for 'sharedcontrols.EmployeeControl.getManagerNames()'
        public void testGetManagerNames() {
            String[] mgrs = employeeControl.getManagerNames();
            assertNotNull("Didn't find managers!", mgrs);
            assertTrue("Found wrong number of managers!", 
                        mgrs.length == 3); 

    (Notice that you were able to use the control simply by using the @Control field notation and didn't have to programmatically instantiate it yourself. That's the magic of ControlTestCase. By extending that class you inherit its setUp() and tearDown() methods that do the declarative wire-up for you.)

  8. Select Ctrl+Shift+S to save your work.

Related Topics

Testing Controls

Click one of the following arrows to navigate through the tutorial:


Skip navigation bar   Back to Top