2

I'm making a light weight painting application with JavaFx. I've been having some issues with my LayerController class and it's methods addLayer etc. So I thought that writing some JUunit test cases would be a good idea to check the correctness of my methods. To make the story short, I'm painting on a Canvas using its GraphicsContext in a selfmade class which I call PaintGraphics. This class does all the painting. The LayerController needs a PaintGraphics to do its work on the layers. But it seems something goes wrong when I initiate a GraphicsContext in a test case. I get the error "Internal graphics not initialized yet.". Which I guess has something to do with the GraphicsContext but I'm not sure. Any ideas about why the error occurs, and how to solve it would be much appreciated!

The source code for the test looks like this:

package view;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Test;

import controller.LayerController;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.AnchorPane;
import model.Layer;
import model.PaintGraphics;

public class LayoutControllerTest {

    Layer layer = new Layer(0, new Canvas(100,100));
    ArrayList<Layer> layers = new ArrayList<Layer>();
    PaintGraphics pGraphics = new PaintGraphics(layer.getCanvas().getGraphicsContext2D());
    LayerController layerController; 

    @Test
    public void addLayerTest() {
        layers.add(layer);
        layerController.addLayer(layer, (AnchorPane)layer.getCanvas().getParent());
    }
}
kryger
  • 12,906
  • 8
  • 44
  • 65
Skarsh
  • 63
  • 1
  • 5
  • Sorry I don't use javafx and don't even have the environment set up so I can try your unit test (In fact, I really don't recognize packages like model.Layer--where is that from??? I would think any Java systems would use better package names than "model", it's indecent.) however the error makes me wonder how you are running JUnit. If you are running it from the command line in Unix, there is a chance that Java doesn't think it has a GUI (running headless), sorry I can't be more help unless I can figure out how to run a self-contained test. – Bill K Sep 19 '16 at 22:19
  • Ah, model is my package, and the Layer class is a class i made myself. This code is not supposed to be able to run for people just dropping by, it's only to give a hint of what my intention with the test case is. The unit is just being run in my IDE, which is Eclipse. This setup works for any other test I've made. The test is not complete, and is not supposed to be working as intended, since the error occurs anyway. – Skarsh Sep 19 '16 at 22:26
  • Oh, sorry, should have figured that out. Well then, I suggest you attempt to create a test case that can be posted here. I don't think you'll have to post it though--the act of creating an isolated, minimal test case is often all you need to do to work out the problem--it's a good troubleshooting technique. – Bill K Sep 19 '16 at 22:41

1 Answers1

5

The exception "Internal graphics not initialized yet." is typically thrown when JavaFX requires the JavaFX platform to be initialized first before using certain features, e.g. Canvas. Approaches to solving this are listed below:

  1. Make a tiny mock application class that extends Application and launch it in a background thread, so the JavaFX Application thread can properly initialize, while you don't block your testing thread.
  2. Use JavaFX testing library, e.g. TestFX
  3. You might be able to mock the canvas object using Mockito
AlmasB
  • 3,377
  • 2
  • 15
  • 17
  • 1
    Since java 9 was released, [`Platform`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/Platform.html) methods are available to both [`startup()`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/Platform.html#startup(java.lang.Runnable)) and [`stop()`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/Platform.html#exit()) the Java platform, so they might be preferable to creating a tiny mock `Application` or using a `JFXPanel` to force the Java platform to start. – jewelsea Jan 19 '22 at 00:39