In 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.
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.
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.