0

So I have a javaFX application with a controller (implements Initializable) with the following initialize method which sets the image of an ImageView.

@FXML
private ImageView logoIMG;

@Override
public void initialize(URL location, ResourceBundle resources) {
    logoIMG.setImage(new Image(getClass().getResource("../../../../resources/main/main/Logo.png").toExternalForm()));
}

I also wrote a test for it to see if the image is actually set:

@Test
void initializeTest() {
    SplashCtrl ctrl = new SplashCtrl(serverMock, mainMock); //also using mockito but that shouldn't be an issue here
    assertEquals(new ImageView(new Image(SplashCtrl.class.getResource("../../../../resources/main/main/Logo.png").toExternalForm())),
            ctrl.getLogoIMG());
}

However this is giving the following exception:

java.lang.RuntimeException: Internal graphics not initialized yet

Could somebody explain to me what's going on and give me a possible solution?

  • 1
    The error message means the JavaFX platform hasn't been started yet (so you can't create some JavaFX nodes). You probably want to call [`Platform.startup(...)`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/Platform.html#startup(java.lang.Runnable)) before running any tests and `Platform.exit(...)` after all are complete. (Or use a JavaFX-specific test framework.) – James_D Mar 09 '22 at 17:48
  • 2
    Off-topic (i.e. not related to your error or question): 1. `..` is not a valid resource name, and this will fail if you try to run it in a jar file. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other. 2. I don't think your `assert.equals(...)` will work. `ImageView` inherits `equals(...)` directly from `Object`, so two `ImageView`s will only be `equal` if they have the same object identity. – James_D Mar 09 '22 at 17:51
  • @James_D starting/closing the platform in BeforeAll and AfterAll indeed fixed the problem with the exception so thanks for that. Now however the actual value of ctrl.getLogoIMG() is null, do you have any idea as to why this could be happening? (I know for sure the image is actually initialized at some point because I see it when running the application) – user16804967 Mar 09 '22 at 18:56
  • 1
    Why would it not be null? There's nothing in your code that indicates it would be initialized unless the controller were managed by an `FXMLLoader`, which it isn't in the test code. – James_D Mar 09 '22 at 18:56
  • Using JUnit to test JavaFX screen widgets is a non-starter in my books. Even more so if what you're trying to test is inside of an FXML Controller class. If the actual problem is that the ImageView isn't getting an Image, and doesn't appear on the screen, then look into that resource. Check to make sure it isn't coming up Null. – DaveB Mar 09 '22 at 22:31
  • 1
    Look into `testfx` which provides APIs (e.g, JUnit 5 extensions) that handle the startup/shutdown of JavaFX for you. Note you don't want to shut the framework down after every test (or even after every test class) because JavaFX cannot be restarted within the same JVM instance. – Slaw Mar 09 '22 at 23:18
  • 1
    @kleopatra The OP calls `toExternalForm()` on the `URL`, which returns the string representation. – James_D Mar 11 '22 at 14:05
  • @James_D darn ... scrolling helps ;) thanks! – kleopatra Mar 11 '22 at 14:46

0 Answers0