Search code examples
javanetbeansjavafx-2javafx-8testfx

@Test Annotiation not working with TestFX


I'm trying to implement TextFX in my project to do some UI testing. However it seems I can't get it to work properly. I've downloaded the jars from http://search.maven.org/#search%7Cga%7C1%7Ctestfx to a folder named 'TestFX-3.1.2' on my system.

Afterwards I've created a new library in Netbeans8 pointing to those jar files (jar, source and javadoc). As a matter of test I've created a simple Java FXML project with the new library added.

public class Test2 extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

Next to that I have a controller for my FXML file with the following generated code:

public class FXMLDocumentController implements Initializable {

@FXML
private Label label;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

To implement the TestFX side, I've created a new class that extends GuiTest:

package test2;

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import org.loadui.testfx.GuiTest;


public class TestTheThing extends GuiTest {

    @Override
    protected Parent getRootNode() {
        FXMLLoader loader = new FXMLLoader();
        Parent node = null;
        try {
            node = loader.load(this.getClass().getResource("FXMLDocument.fxml").openStream());
        } catch (IOException e) {
            System.out.println(e.toString());    
        }
        return node;
    }

    @Test //<-- this Annotiation does not work
    public void pressTheButton(){
        //TODO
    }
}

As said above in the code, the @Test simply does not work and is red underlined with the warning 'cannot find symbol'. Could anyone point me in the right direction about what I'm doing wrong?


Solution

  • According to https://repo1.maven.org/maven2/org/loadui/testFx/3.1.2/testFx-3.1.2.pom, testFx has several dependencies (guava, junit, hamcrest-all, hamcrest-core). To work correctly, you need to add the jars corresponding to these dependencies to your project. However, using maven is the recommended approach for that.