第一步是在你的 fxml 文件中给你的组件 fx:id-s,然后是这样的:
public class ChangeProfilesMenuControllerTest extends ApplicationTest {
Pane mainroot;
Stage mainstage;
@Override
public void start(Stage stage) throws IOException {
mainroot = (Pane) FXMLLoader.load(Main.class.getResource("ChangeProfilesMenU.fxml"));
mainstage = stage;
stage.setScene(new Scene(mainroot));
stage.show();
stage.toFront();
}
@Before
public void setUp() throws Exception{
}
@After
public void tearDown () throws Exception {
FxToolkit.hideStage();
release(new KeyCode[]{});
release(new MouseButton[]{});
}
@Test
public void addingAndDeletingProfiles() {
@SuppressWarnings("unchecked")
ListView<String> listview = (ListView<String>) mainroot.lookup("#listview");
clickOn("#textfield");
write("This is a test");
clickOn("#createnewprofile");
...
}
如果你想访问你的控制器类实例:
@Override
public void start(Stage stage) throws IOException {
this.mainstage = stage;
FXMLLoader loader = new FXMLLoader(getClass().getResource("GameOn2.fxml"));
this.mainroot = loader.load();
this.controller = loader.getController();
stage.setScene(new Scene(mainroot));
stage.show();
stage.toFront();
}