Basic JUnit test for JavaFX 8

后端 未结 3 867
难免孤独
难免孤独 2020-11-27 19:19

I want to create basic JUnit test for JavaFX 8 application. I have this simple code sample:

public class Main extends Application {
    public static void ma         


        
3条回答
  •  余生分开走
    2020-11-27 19:47

    The easiest aproach is the following:

    import javafx.application.Platform;
    import javafx.embed.swing.JFXPanel;
    import javafx.stage.Stage;
    
    import org.junit.Test;
    
    public class BasicStart {
    
        @Test
        public void testA() throws InterruptedException {
            Thread thread = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    new JFXPanel(); // Initializes the JavaFx Platform
                    Platform.runLater(new Runnable() {
    
                        @Override
                        public void run() {
                            new Main().start(new Stage()); // Create and
                                                            // initialize
                                                            // your app.
    
                        }
                    });
                }
            });
            thread.start();// Initialize the thread
            Thread.sleep(10000); // Time to use the app, with out this, the thread
                                    // will be killed before you can tell.
        }
    
    }
    

    Hope it helps!

提交回复
热议问题