主要考慮的是考慮在FX線程內運行測試。當你創建一個類擴展應用程序時,你實際上創建了一個過程。這是你想測試的。
因此,要在應用程序上啓動一些單元測試,首先創建一個擴展應用程序的FXAppTest,然後在FXAppTest中啓動單元測試。這是主意。
以下是JUnit的示例。我創建了一個在FXApp內部進行測試的Runner進行測試。 下面是對FxApplicationTest代碼(我們推出它裏面的單元測試)的一個例子
public class FxApplicationTest extends Application {
private volatile boolean isStopped;
@Override
public void start(final Stage stage) {
StackPane root = new StackPane();
Scene scene = new Scene(root, 10, 10);
stage.setScene(scene);
}
public void startApp() {
launch();
}
public void execute(final BlockJUnit4ClassRunner runner, final RunNotifier notifier) throws InterruptedException {
isStopped = false;
Platform.runLater(new Runnable() {
@Override
public void run() {
runner.run(notifier);
isStopped = true;
}
});
while (!isStopped) {
Thread.sleep(100);
}
}
和轉輪:
import fr.samarie_projects.fx.utils.JUnitFxRunner;
@RunWith(JUnitFxRunner.class)
public class MainFxAppTest {
@org.junit.Test
public void testName() throws Exception {
MainFxApp fxApp = new MainFxApp();
fxApp.start(new Stage());
}
}
:
import org.apache.log4j.Logger;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
public class JUnitFxRunner extends Runner {
private final BlockJUnit4ClassRunner runner;
private final Logger LOGGER = Logger.getLogger(JUnitFxRunner.class);
public JUnitFxRunner(final Class<?> klass) throws InitializationError {
super();
runner = new BlockJUnit4ClassRunner(klass);
}
@Override
public Description getDescription() {
return Description.EMPTY;
}
@Override
public void run(final RunNotifier notifier) {
try {
final FxApplicationTest fxApplicationTest = new FxApplicationTest();
MyTestRunner runnable = new MyTestRunner(runner, notifier, fxApplicationTest);
new Thread(runnable).start();
Thread.sleep(100);
runnable.execute();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
private class MyTestRunner implements Runnable {
private final BlockJUnit4ClassRunner runner;
private final RunNotifier notifier;
private final FxApplicationTest fxApp;
public MyTestRunner(final BlockJUnit4ClassRunner runner, final RunNotifier notifier, final FxApplicationTest fxApp) {
this.runner = runner;
this.notifier = notifier;
this.fxApp = fxApp;
}
@Override
public void run() {
fxApp.startApp();
}
public void execute() throws InterruptedException {
fxApp.execute(runner, notifier);
}
}
}
現在,只需使用亞軍發射試驗
此單元測試MainFxApp
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainFxApp extends Application {
@Override
public void start(final Stage stage) throws Exception {
StackPane root = new StackPane();
Scene scene = new Scene(root, 10, 10);
stage.setScene(scene);
}
public static void main(final String[] args) {
launch(args);
}
}
當然,這個代碼需要審查。只是提出這個想法。
您可能還需要看看JavaFX團隊用來測試他們的軟件的[JemmyFX visual testing framework](http://fxexperience.com/2012/02/announcing-jemmyfx/)。 – jewelsea
JemmyFX的更好地址:http://jemmy.java.net/JemmyFXGuide/jemmy-guide.html – assylias