如何对 javafx 进行 junit 测试

How to do junit test for javafx

我正在尝试 运行 javafx junit 测试。但是,我遇到了问题,没有 运行 在 javafx 线程上进行测试。所以我做了这个问题答案所说的(Basic JUnit test for JavaFX 8)。但是,现在我在我的 junit class 中遇到了这个错误,我不知道如何修复它

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: javafx/embed/swing/JFXPanel
    at TestJunit.run(TestJunit.java:26)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.ClassNotFoundException: javafx.embed.swing.JFXPanel
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 2 more

还有我的 junit 文件,如果有帮助的话。

import org.junit.Test;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import Champions.autoBattleChampion;
import Model.autoBattleModel;

import static org.junit.Assert.assertEquals;

import org.junit.BeforeClass;
import org.junit.Rule;

public class TestJunit {


   /*
    * <== Test Model ==>
    */
   @Test
   public void testpopulateChampionStore_0() {  
       Thread thread = new Thread(new Runnable() {

           @Override
           public void run() {
               new JFXPanel(); // Initializes the JavaFx Platform
               Platform.runLater(new Runnable() {

                   @Override
                   public void run() {
                      autoBattleModel model = new autoBattleModel();
                      model.stopTimer();
                      assertEquals(1,1);

                   }
               });
           }
       });
       thread.start();// Initialize the thread
       try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // Time to use the app, with out this, the thread
                               // will be killed before you can tell.
   }
}

将这些 vm 参数添加到我的 junit class 修复了它。

--module-path /path/to/javafx-sdk-17/lib --add-modules javafx.controls,javafx.fxml