yel*_*von 2 java testing unit-testing javafx testfx
我在使用Oracle的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();
}
}
Run Code Online (Sandbox Code Playgroud)
TestFx junit测试:
class MyTest extends GuiTest {
public Parent getRootNode() {
return nodeUnderTest;
}
Run Code Online (Sandbox Code Playgroud)
nodeUnderTest这个例子应该是什么?
TestFx是一个单元测试框架,因此它旨在抓取部分GUI实现并对其进行测试.这要求您首先提供这些部件,并通过使用ID标记它们来测试目标(按钮等).
getRootNode()为GUI测试的以下测试过程提供了根.在上面的示例中,StackPane根可能是候选者...但是这要求您使其可用于测试以允许:
class MyTest extends GuiTest {
public Parent getRootNode() {
HelloWorld app = new HelloWorld();
return app.getRoot(); // the root StackPane with button
}
}
Run Code Online (Sandbox Code Playgroud)
因此,必须修改应用程序以实现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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6996 次 |
| 最近记录: |