我正在尝试在我的项目中实现TextFX来进行一些UI测试。然而,我似乎无法使它正常工作。我已经将http://search.maven.org/#search%7Cga%7C1%7Ctestfx中的jars下载到了我的系统上名为“TestFX-3.1.2”的文件夹中。
之后,我在Netbeans8中创建了一个指向这些jar文件(jar、source和javadoc)的新库。作为一个测试问题,我创建了一个简单的Java项目,并添加了新的库。
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);
}
}接下来,我使用以下生成的代码为我的FXML文件提供了一个控制器:
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
}
}为了实现TestFX端,我创建了一个扩展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
}
}正如上面在代码中所说的,@Test根本不起作用,并在红色下划线上加上警告‘无法找到符号’。有人能指出我做错了什么吗?
发布于 2015-02-19 15:28:09
根据https://repo1.maven.org/maven2/org/loadui/testFx/3.1.2/testFx-3.1.2.pom的说法,testFx有几个依赖项(番石榴、junit、hamcrest-all、hamcrest)。要正确工作,需要将对应于这些依赖项的jars添加到项目中。然而,使用maven是推荐的方法。
发布于 2015-02-26 07:47:30
不要直接在测试类中加载fxml文件,因为它可能不是有意的。相反,以这种方式启动主类:
FXTestUtils.launchApp(Test2.class);
Thread.sleep(2000);
controller = new GuiTest()
{
@Override
protected Parent getRootNode()
{
return Test2.getStage().getScene().getRoot();
}
};在您的static类中创建一个返回这个阶段的getStage()方法。上面的代码应该驻留在测试类中使用@BeforeClass的方法中。控制器是对GuiTest的静态引用。
最后,您的测试类应该如下所示:
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");
}
}在这种情况下,您不需要从GuiTest扩展。而且,不要忘记在static类中创建getStage()。希望这能帮上忙。在我的情况下,一切都很顺利。
https://stackoverflow.com/questions/28610085
复制相似问题