0

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?

gontard
  • 28,720
  • 11
  • 94
  • 117
Perneel
  • 3,317
  • 7
  • 45
  • 66
  • You probably have an issue with the jar dependency. Are you using maven and do you have a .pom for your project? – alainlompo Feb 19 '15 at 15:25
  • No I don't use Maven, that's why I just downloaded the jars in the hope that it would work. – Perneel Feb 19 '15 at 15:27

2 Answers2

3

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.

Guy Bouallet
  • 2,099
  • 11
  • 16
2

Don't load your fxml file directly in the test class as it may not work intentionally. Instead launch main class this way:

FXTestUtils.launchApp(Test2.class);
Thread.sleep(2000);

controller = new GuiTest()
    {
        @Override
        protected Parent getRootNode()
        {
            return Test2.getStage().getScene().getRoot();
        }
    };

Create a static method getStage() in your Test2 class that returns the Stage. The above code should reside in a method annoted with @BeforeClass in your test class. The controller is a static reference to the GuiTest.

In the end your test class should look something like this:

import java.io.IOException;

import javafx.scene.Parent;

import org.loadui.testfx.GuiTest;
import org.loadui.testfx.utils.FXTestUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestTheThing
{
    public static GuiTest controller;

@BeforeClass
public static void setUpClass() throws InterruptedException, IOException
{
    FXTestUtils.launchApp(Test2.class);
    Thread.sleep(2000);
    controller = new GuiTest()
    {
        @Override
        protected Parent getRootNode()
        {
            return Test2.getStage().getScene().getRoot();
        }
    };  
}

@Test
public void testCase()
{
    System.out.println("in a test method");
}
}

In this case you will not need to extend from GuiTest. And, don't forget to create static getStage() in Test2 class. Hope this will help. This is working fine in my case.

Kuldeep Verma
  • 104
  • 1
  • 12