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:
javafx/**
(right-click on project -- properties -- Java Build Path -- Libraries -- JRE System Library -- Access Rules -- add).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(); } }
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;Then I tried to extend the class MyTest to use testfx:
class MyTest extends GuiTest {}
as advised in this link; and (separately)
class MyTest extends ApplicationTest{}
as advised in this link.
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?
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:
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.+"
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.