Search code examples
javaeclipsejavafxtestfx

How do I install TestFX?


I'd like to put together a set of jUnit tests under Eclipse (Neon) on Windows to automate the testing of JavaFX GUIs. It seems that TestFX is the bee's knees for this kind of thing, but having looked around the internet quite a bit, I'm still not sure how to install TestFX without using Maven or Gradle.

I am not familiar with Maven and Gradle, and trying to follow the simple instructions to install TestFX via Maven was unsuccessful. This was done under Eclipse Mars, after which my simple GUI program threw up a compile error about not being able to find or load main class and a run-time error that Selection does not contain a main type. (The simple GUI program ran without errors previously.) After this, I downloaded the latest Eclipse Neon and tried to start afresh.

This is what I did:

  1. Download and install Eclipse Neon from eclipse.org.
  2. Create a Java Project called TestProject (execution environment JavaSE-1.8).
  3. Grant access to javafx/** (right-click on project -- properties -- Java Build Path -- Libraries -- JRE System Library -- Access Rules -- add).
  4. Create MyClass with minimal contents:
package test;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyClass extends Application {
  @Override
  public void start(Stage stage) throws Exception {
      stage.setTitle("Hello World");
      stage.show();
  }
}
  1. Copy the file testfx-core-4.0.0-20150226.214553-8.jar from the testfx repository (linked from the Via direct download section of How to use TestFX in your project) into my eclipse project, sitting at the same level as the JRE System Library;
  2. Add the jar file to the build path (right-click on project -- properties -- Java Build Path -- Libraries -- Add JARS) - this automatically created a directory called Referenced Libraries and copied the jar file into it; and
  3. Create a JUnit test (right-click on source folder -- New -- JUnit Test Case), filling in the appropriate package and Class under test fields, and giving the test case a name (MyTest).

Then I tried to extend the class MyTest to use testfx:

Here's the code in the second case:

package test;
import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;
public class MyTest extends ApplicationTest { }

In each case, eclipse complains that the superclass GuiTest / ApplicationTest cannot be resolved to a type.

I suspect that the problem is that I haven't properly installed testfx. Can anyone help?


Solution

  • There are multiple dependencies; the following jar files must be added to the Referenced Libraries (after copying the jar files into the project, right-click on the project -- Properties -- Java Build Path -- Libraries -- Add JARs...).

    The above is independent of Gradle and Maven. Alternatively, the files can also be pulled using Gradle:

    1. Create a Gradle project in Eclipse.
    2. In the dependencies block in build.gradle, insert the following lines (source):

      testCompile "org.testfx:testfx-core:4.0.+"
      testCompile "org.testfx:testfx-junit:4.0.+"
      
    3. Right-click the project -- Gradle -- Refresh Gradle Project

    This places the required files into the Project and External Dependencies folder.

    Useful tutorial for TestFX 4: https://www.youtube.com/watch?v=NG03nNpSmgU.