0

I have a test class that has several tests and all pass. If I add the class to a test suite, all tests fail.

The reason for that is that dependencies to JavaFX types cannot be resolved. I use a test rule that initializes the JavaFX framework (e.g. the application thread etc.) and this seems to work fine as the passed tests indicate. The tests fail due to an exception:

java.lang.NoClassDefFoundError: Could not initialize class javafx.scene.control.Label

Why does JUnit behave differently depending on how I run tests? How can I fix this? Note that this is not in particular a GUI test per so (I do not want to simulate user actions or something like this, it is more about how certain controls are created and managed internally by my code).

Test suite:

@RunWith(Suite.class)
@SuiteClasses({ 
    TestClass.class
})

public class TestSuite {

}
Spiegelritter
  • 806
  • 1
  • 7
  • 16

1 Answers1

0

You are probably running your test suite under Java 7 (where you need to manually add the JavaFX runtime to the class path).

I recommend doing all JavaFX development on a minimum Java release of Oracle Java 8 (where all of the JavaFX toolkit will automatically be on the default class path).

For more info see the answer to: Compile code using JavaFX 2.0

System.getProperty("java.version") returns 1.8.0_25, i.e. the version should not be a problem.

Could also be that you are using an OpenJDK build rather than an Oracle JDK build (as most OpenJDK implementations don't currently include JavaFX).

A likely issue is that your test suite is being run using a different class loader than the default class loader and the class loader the test suite is using does not include the JavaFX runtime classes (which in Java 8 are in the <JRE_HOME>/lib/ext/jfxrt.jar).

If you wish, you could edit your question to include an MCVE so that somebody could replicate your problem.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406