Geertjan is a DZone Zone Leader and has posted 466 posts at DZone. You can read more from them at their website. View Full User Profile

Functional Tests in a Maven-Based NetBeans Platform Application

12.03.2010
| 7166 views |
  • submit to reddit

Let's create a Maven-based application on the NetBeans Platform and set up some functional tests. So, we will create an automated test environment for the UI of our application. The starting point for these kinds of tests is to design a few typical scenarios that your end users are going to step through in their day-to-day work with your application. Then, you need to create functional tests that will simulate those typical scenarios.

Fortunately, the NetBeans Platform comes with a package that extends JUnit, named org.netbeans.junit, with a lot of helpful test-related support classes. Specifically for functional testing, there's the org.netbeans.jellytools package. Let's use the latter to set up our functional tests.

  1. Install NetBeans IDE 7.0 Beta or above.

  2. Create a new Maven-based NetBeans Platform application via the related template in the IDE.

  3. Open the POM file of the application, where you will find some sections that have been commented out. Remove the comment-out lines and you will end up with these dependencies in the application POM:
    <dependency>
        <groupId>org.netbeans.api</groupId>
        <artifactId>org-netbeans-modules-nbjunit</artifactId>
        <version>${netbeans.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.netbeans.api</groupId>
        <artifactId>org-netbeans-modules-jellytools-platform</artifactId>
        <version>${netbeans.version}</version>
        <scope>test</scope>
    </dependency>

  4. You will also have the SureFire section included, in the "build" section of the application POM, again by removing the line that comments the section below out:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <systemProperties>
                <property>
                    <name>cluster.path.final</name>
                    <value>${project.build.directory}/${brandingToken}/${brandingToken}:${project.build.directory}/${brandingToken}/platform</value>
                </property>
            </systemProperties>
        </configuration>
    </plugin>

  5. In one of the module's of your application, do some stuff:
    public class DoStuff {
        
        public static void stuff(String label) {
            TopComponent tc = new TopComponent();
            tc.setName(label);
            tc.open();
        }
    
    }

    Note: The above code comes from Jesse's attachment in the issue about functional testing for Maven-based NetBeans Platform applications.


  6. Now create a new test in the application's "src/java/test" folder, as follows:
    import junit.framework.Test;
    import org.netbeans.jellytools.JellyTestCase;
    import org.netbeans.jellytools.OptionsOperator;
    import org.netbeans.junit.NbModuleSuite;
    
    public class FuncTest extends JellyTestCase {
    
        public static Test suite() {
            return NbModuleSuite.allModules(FuncTest.class);
        }
    
        public FuncTest(String n) {
            super(n);
        }
    
        public void testWhatever() throws Exception {
            DoStuff.stuff("hello");
            OptionsOperator.invoke().selectMiscellaneous();
            Thread.sleep(5000);
            System.err.println("OK.");
        }
        
    }

    Via the org.netbeans.jellytools.OptionsOperator, you gain access to the Options window in your application.


  7. Right-click the application, choose Test, and then your tests will start up. The application starts, a new window is created (with "hello" as the display name), the "Miscellaneous" tab of the Options window is opened, there's a thread sleep, and an OK message.

    You'll see the result in the IDE. If you get stuck setting up the above scenario, here's a screenshot of everything:

Documentation on all the various operators are found in the NetBeans Javadoc for the Jelly Tools Platform:

http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-jellytools-platform/overview-summary.html

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Chris Heidt replied on Fri, 2011/02/25 - 2:46pm

I'm having trouble with this.
At first I though it was because It was trying to get by with using 6.9.1 instead of the 7.0 beta,
but even after upgrading my tests dont appear to run.
Does the project itself have to be using platform RELEASE70-BETA?

EDIT: Nope, upgrading my app to RELEASE70-BETA gave me the same result... my tests dont run :(

Jesse Glick replied on Mon, 2011/04/04 - 11:02am in response to: Chris Heidt

As of the 3.5 plugin you need to use the integration-test phase, not test. (I will fix the IDE in a post-7.0 update to use this goal when Test Project is selected.)

Mateo Gomez replied on Mon, 2012/08/20 - 1:45am

maven based are so awesome

chicken burrito recipe

Matt Coleman replied on Tue, 2012/08/21 - 1:21am in response to: Jesse Glick

i agree...integrations-test best is way better

graphic artist buffalo

Cata Nic replied on Mon, 2013/09/02 - 4:38pm

 The test results can be a good reason to use this solution instead of other complicated ones. I prefer NetBeans anyway...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.