Home > Mobile >  How to do junit test for javafx
How to do junit test for javafx

Time:12-11

I was trying to run javafx junit test. However, I had issues with it not running the test on the javafx thread. So I did what this questions answers said to do (Basic JUnit test for JavaFX 8). However, now I'm getting this error in my junit class and I don't how to fix it

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: javafx/embed/swing/JFXPanel
    at TestJunit$1.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

Also heres my junit file if it helps.

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.
   }
}

CodePudding user response:

Adding these vm arguments into my junit class fixed it.

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