Improving our JUnit tests

JunitIn the final article of this series we will cover ways of improving our JUnit tests functionality. During my time writing and improving our internal testing I have found several things that can make writing your own tests faster, easier and help you avoid unnecessary complications. Below I have detailed the three most useful improvements I have found.

 

Comparing image output

Whilst testing GUI functionality you will most likely be creating an image of the application and comparing the image to a known baseline to test for changes. This is a relatively simple task, as I mentioned previously, as you need only to loop through the colour values of the pixels in the output and baseline. Should these two values match match each other you have found an issue. This being said certain image formats should be avoided if they involve lossy compression as the values may change subtly.

You should note that taking screenshots should be done on the Event thread, especially if you are using volatile images for any part of the rendering. If you do not you may find that the colour values change slightly each time you attempt to make an image.

 

Utility classes

Chances are you will be writing multiple tests all performing similar tasks but on different files and GUI features. One of the best ways of I have found of improving our JUnit tests is to set up a utility class to handle all common input and output tasks. By creating a utility class with each basic type of input and output being accessed by a static method this allows every tests to simply be a combination of pre-existing methods with no need to reinvent the wheel each time.

This idea has made it much faster to develop new tests as we have discovered they were required. Some functionality that works well in a utilities class includes methods to get a screenshot, enter text, click a screen / application location, file opening and application closing.

 

Typing with a Robot class

It is not possible to just loop through a String and pass in the int value of each character if you are using a Robot to control keyboard and mouse input for your tests. The Robot class mimics the keyboard input exactly meaning each key, not character, can be pressed and released. Therefore you can not just pass in the value of the character ‘_’, you actually would need to first have the Robot press the shift key then the ‘-‘ key then release both. This is because the Robot works as a literal virtual keyboard and the character int values represent the keys pressed.

 

This is the final article of this series. With this and the previous two articles ‘Running tests on both JavaFX and Swing using Junit‘ and ‘Creating easily extendible Tests in Java with Junit‘ you should have the basics needed to set up your own suite of tests in a way that can easily be expanded upon.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.
Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.
KieranF

About Kieran France

Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.

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=""> <s> <strike> <strong>