我在使用甲骨文的JavaFx HelloWorld应用程序时遇到了一些问题:
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}TestFx junit测试:
class MyTest extends GuiTest {
public Parent getRootNode() {
return nodeUnderTest;
}对于这个例子,nodeUnderTest应该是什么?
发布于 2014-12-10 15:47:28
TestFx是一个单元测试框架,所以它被设计用来抓取你的图形用户界面实现的一部分并在上面进行测试。这要求您首先使这些部件可用,并通过使用ids标记它们来使测试目标(按钮等)可用。
getRootNode()为下面的图形用户界面测试过程提供了根。在上面的示例中,StackPane根可能是一个候选...但这需要您将其提供给测试,以允许:
class MyTest extends GuiTest {
public Parent getRootNode() {
HelloWorld app = new HelloWorld();
return app.getRoot(); // the root StackPane with button
}
}因此,必须修改应用程序以实现getRoot(),返回带有测试内容的StackPane,而不需要使用start()方法。
你可以在上面运行测试...
@Test
public void testButtonClick(){
final Button button = find("#button"); // requires your button to be tagged with setId("button")
click(button);
// verify any state change expected on click.
}发布于 2015-01-07 00:47:55
还有一种简单的方法来测试整个应用程序。为了确保您的应用程序被正确初始化和启动,它需要由JavaFX应用程序启动器启动。不幸的是,TestFX不支持开箱即用(至少我还没有找到这样做的方法),但是你可以通过继承GuiTest的子类来轻松地做到这一点:
public class HelloWorldGuiTest extends GuiTest {
private static final SettableFuture<Stage> stageFuture = SettableFuture.create();
protected static class TestHelloWorld extends HelloWorld {
public TestHelloWorld() {
super();
}
@Override
public void start(Stage primaryStage) throws IOException {
super.start(primaryStage);
stageFuture.set(primaryStage);
}
}
@Before
@Override
public void setupStage() throws Throwable {
assumeTrue(!UserInputDetector.instance.hasDetectedUserInput());
FXTestUtils.launchApp(TestHelloWorld.class); // You can add start parameters here
try {
stage = targetWindow(stageFuture.get(25, TimeUnit.SECONDS));
FXTestUtils.bringToFront(stage);
} catch (Exception e) {
throw new RuntimeException("Unable to show stage", e);
}
}
@Override
protected Parent getRootNode() {
return stage.getScene().getRoot();
}
// Add your tests here
}https://stackoverflow.com/questions/27387116
复制相似问题