JUunit测试用例中的内部图形尚未初始化 [英] Internal graphics not initialized yet in JUunit test case

查看:100
本文介绍了JUunit测试用例中的内部图形尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JavaFx制作一个轻量级的绘画应用程序. 我的LayerController类及其方法addLayer等一直遇到问题.因此,我认为编写一些JUunit测试用例是检查方法正确性的一个好主意.为了使故事简短,我在一个称为PaintGraphics的自制类中使用它的GraphicsContextCanvas上绘画.这堂课负责所有绘画. LayerController需要PaintGraphics才能在层上进行工作.但是当我在测试用例中启动GraphicsContext时,似乎出了点问题.我收到错误内部图形尚未初始化".我想这与GraphicsContext有关,但我不确定.感谢您提供任何关于为什么会发生错误以及如何解决该错误的想法!

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!

测试的源代码如下:

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());
    }
}

推荐答案

异常内部图形尚未初始化." 通常是在JavaFX要求先使用特定的JavaFX平台进行初始化然后再使用某些特定语言时引发的功能,例如Canvas.下面列出了解决此问题的方法:

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. 制作一个扩展了Application的微型模拟应用程序类,并在后台线程中启动它,以便JavaFX Application线程可以正确初始化,而又不会阻塞测试线程.
  2. 使用JavaFX测试库,例如 TestFX
  3. 您也许可以使用 Mockito
  4. 模拟画布对象
  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

这篇关于JUunit测试用例中的内部图形尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆