I have a JUnit Test Class that looks like the following :
public class HelloWorldApplicationTest {
private static TextArea mEditor = new HelloWorldApplication().getmEditor();
@BeforeClass
public static void initToolKit() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
SwingUtilities.invokeLater(() -> {
new JFXPanel(); // initializes JavaFX environment
latch.countDown();
});
// That's a pretty reasonable delay... Right?
if (!latch.await(5L, TimeUnit.SECONDS))
throw new ExceptionInInitializerError();
}
@Test
public void testA(){
mEditor.setText("111");
assertTrue(mEditor.getText().equals("111"));
mEditor.setText("qqq");
assertTrue(mEditor.getText().equals("qqq"));
mEditor.setText("eee");
assertTrue(mEditor.getText().equals("eee"));
mEditor.setText("aaa");
assertTrue(mEditor.getText().equals("aaa"));
mEditor.setText("aaa");
assertTrue(mEditor.getText().equals("aaa"));
}
@Test
public void testB(){
mEditor.setText("222");
assertTrue(mEditor.getText().equals("222"));
mEditor.setText("222");
assertTrue(mEditor.getText().equals("222"));
mEditor.setText("222");
assertTrue(mEditor.getText().equals("222"));
mEditor.setText("111");
assertTrue(mEditor.getText().equals("111"));
}
@Test
public void testC(){
mEditor.setText("333");
assertTrue(mEditor.getText().equals("333"));
mEditor.setText("333");
assertTrue(mEditor.getText().equals("333"));
mEditor.setText("TTT");
assertTrue(mEditor.getText().equals("TTT"));
mEditor.setText("333");
}
}
If I remove initToolkit
method, tests will pass in Linux / OSX , but fail in windows throwing :
java.lang.ExceptionInInitializerError: null
at com.sun.glass.ui.Screen.getScreens(Screen.java:70)
at com.sun.glass.ui.Screen.getMainScreen(Screen.java:61)
at com.sun.javafx.font.PrismFontFactory.getSystemFontSize(PrismFontFactory.java:1911)
Or , mEditor isn't a static variable.
java.lang.RuntimeException: Internal graphics not initialized yet
at com.sun.glass.ui.Screen.getScreens(Screen.java:70)
at com.sun.glass.ui.Screen.getMainScreen(Screen.java:61)
at com.sun.javafx.font.PrismFontFactory.getSystemFontSize(PrismFontFactory.java:1911)
(See : JavaFX Test That fails in Windows and works in OSX and Linux )
If I add initToolkit
method, JavaFX environment get initialised, but tests start to fail randomly, and sometimes tests pass !
When I tested on a windows VM with a single processor , tests pass, which could describe here a multithreading issue over static editor ?
I've tried synchronized blocks (synchronised(mEditor)
) , initializing JavaFX environement by refering to this thread : How do you unit test a JavaFX controller with JUnit, but I always end up with this random behaviour, in which some tests pass, some tests fail and if run again tests could pass or fail randomly.