Based on some of the Sun JavaFX forums questions, there is uncertainty around writing unit tests for JavaFX classes. Since unit testing is an important part of a balanced developer diet, this post aims to remedy that. I will show you how to write unit tests for your JavaFX classes and even show you how to run them!

To write unit tests, we’re going to use the popular Java unit testing framework, JUnit. Most of the steps in this article will be fairly familiar to those who’ve written unit tests for JUnit 3.x before. This post assumes you will be using NetBeans to write your unit tests.

Getting Started

Before you can write unit tests, you’ll need to update your project configuration first. You need to add the JUnit 3.x to your project’s library. To do so in NetBeans, follow these steps:

  • Right click on your Libraries folder from your “Projects” view of your project.
  • Select “Add Library”.
  • In the resulting dialog, select JUnit 3.x or 4.x (e.g. JUnit 3.8.2) and click “Add Library.”

Now you’re ready to start writing tests!

The Functionality

Before we start writing our test cases, let’s write our functionality first. We’re going to write a simple JavaFX class that produces the value of the Nth Fibonacci number. Create a new JavaFX class called net.newfoo.fib.Calculator and enter the following text:

  package net.newfoo.fib;

public class Calculator { public function fib( n : Integer ) : Integer { if (n <= 1) { return n; } else { return fib(n – 2) + fib(n – 1); } } }

Writing The Tests

If you’ve written JUnit tests before, these steps will be pretty familiar. To write JUnit test cases, we’ll need to create a class that extends the class junit.framework.TestCase. For each test case, we will add a class method in the form of testYYY(). The only differences between a Java JUnit test and a JavaFX JUnit test pertain to language differences between Java and JavaFX. In particular, our “test” methods and the variables we use will be defined in JavaFX syntax. Every other aspect of JUnit testing will be the same in JavaFX as it is for Java. Here is an example test in both Java and JavaFX that tests whether 2 + 2 = 4:

Java Version:

  import junit.framework.TestCase;

public class MyTest extends TestCase { public void testSomething() { int x = 2; int y = 2; assertEquals(4, x + y); } }

JavaFX Version:

  import junit.framework.TestCase;

public class MyTest extends TestCase { // function declaration is different public function testSomething() : Void { // variable declarations are different. var x : Integer = 2; var y : Integer = 2; assertEquals(4, x + y); } }

As you can see by the code snippets, the code is similar with the exception of how methods are defined and variables are declared. Now let’s write two JavaFX test cases to test our Fibonacci calculator. One test will confirm the value of fib(3) and one test will confirm the value of fib(5). To write your test cases, create a new JavaFX class called new.newfoo.fib.FibTests and type in the following:

  package net.newfoo.fib;

import junit.framework.TestCase; import junit.textui.TestRunner; // more on this in a moment public class FibTests extends TestCase { function testFib3() { var f = Calculator{ }; assertEquals(2, f.fib(3)); } function testFib5() { var f = Calculator{ } assertEquals(5, f.fib(5)); } }

In the example above, we assert fib(3) is equal to 2 and that fib is equal to 5. The assertEquals() is one of the many useful methods that come from TestCase. When the assertion fails, the test fails and a stack trace is printed in the test output.

Running The Tests

Now that we wrote our tests, how do we actually run them? If you’ve written JUnit tests in Java using a popular IDE, you’ve probably come across a handy test runner. Unfortunately at the time of this writing, I cannot find any easy way to use NetBeans UI to run JavaFX test cases. I found that by using JUnit’s TestRunner facility though, we can still run our tests fairly easily from the IDE. We can setup FibTests to run by adding a small function to the FibTests.fx file that can be executed by the JavaFX runtime. Add the following code to the bottom of FibTests.fx:

  function run() {
      var r : TestRunner = TestRunner{ };
      r.run(r.getTest(“net.newfoo.fib.FibTests”));
  }

The above code creates a new JUnit TestRunner and adds all of the tests defined in the FibTests class (in this case the two we wrote above). We can execute the tests now by either right clicking on FibTests.fx in the Projects view and selecting “Run File” OR by pressing Shift+F6 while we have FibTests.fx open in our editor.

After running the tests, you should see a bunch of output including the words “OK (2 tests)” towards the bottom of your console. This output tells you that your two tests passed. Experiment by breaking your Fib class implementation and re-running the tests. You should see test failures when you re-run the tests.

Conclusion

I hope you found this method of writing JUnit tests in JavaFX straight-forward. As people begin to write more complicated JavaFX applications, proper unit testing will be a powerful tool to keep our application quality up to the standards people have come to expect of Java applications.

If you’re not already familiar with JUnit or unit testing in general, you should check out the Test Infected JUnit tutorial to learn more. Though the Test Infected tutorial pertains to writing unit tests in Java, the material is applicable to JavaFX if you make sure you define your functions and variables using JavaFX syntax instead of Java syntax.

comments powered by Disqus