How to use Jemmy with JUnit in NetBeans

We are always constantly looking to improve and refine our testing. Currently we are looking into improving our tests for forms in our PDF viewer.

For the GUI testing we are using Jemmy, a library that comes with the NetBeans IDE and is very useful for testing Swing applications. NetBeans also comes with JUnit bundled in for unit testing that we also make use of. If you are writing a module and want to use Jemmy this is a good place to start. However if you want to use Jemmy with a project as opposed to a module you will need to remember to add it to the classpath.

So we wanted to have our Jemmy tests run as part of our unit testing but there wasn’t much in the way of documentation on how to do this.

Upon investigation it is actually very easy to get them working together. What you need to do is simply write JUnit tests that utilize Jemmy so for example:

@Test
public void JunitTest() {
JFrameOperator mainFrame = new JFrameOperator();
JTextFieldOperator textField = new JTextFieldOperator(mainFrame, "textIn");
int x = 10;
assertEquals(x, textField.getLocationOnScreen().x);
}

Adding the @Test annotation and making use of JUnit’s assertEquals() and fail() if needed.

And that’s pretty much it!

One thing worth mentioning is that when reading/writing text files for loading test data it’s important to make sure you explicitly set the encoding. As I found that when running Jemmy as a standalone test, not using the test option in NetBeans and not as part of a JUnit test, it used a different default encoding to when using the test option and running it as part of our unit testing.

What methods do you use to improve your testing?

This post is part of our “NetBeans article Index” series. In these articles, we aim to explore NetBeans in different ways, from useful hint and tips, to our how-to’s, experiences and usage of the NetBeans IDE.

The following two tabs change content below.
Lyndon is a Developer at IDR Solutions. He currently focuses mostly on the JavaScript in the Viewer and PDF to HTML5 Converter and also the Android PDF Viewer. He gave a short talk at the GlassFish UnConference before JavaOne 2012. Outside of IDR Solutions he has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

Related Posts:

lyndon

About Lyndon Armitage

Lyndon is a Developer at IDR Solutions. He currently focuses mostly on the JavaScript in the Viewer and PDF to HTML5 Converter and also the Android PDF Viewer. He gave a short talk at the GlassFish UnConference before JavaOne 2012. Outside of IDR Solutions he has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>