How do you mock a JavaFX toolkit initialization?(你如何模拟 JavaFX 工具包初始化?)
问题描述
[序言:抱歉,这里有很多代码,其中一些可能与这个问题无关,而一些理解问题所必需的代码可能会丢失;请发表评论,我会相应地编辑问题.]
[preamble: apologies, there is a lot of code here, and some of it may not be relevant to this question while some code which is necessary to understand the problem may be missing; please comment, and I will edit the question accordingly.]
环境:Ubuntu 14.10 x86_64;甲骨文 JDK 1.8u25.单元测试库是TestNG,版本6.8.13;Mockito 是 1.10.17 版本.
Environment: Ubuntu 14.10 x86_64; Oracle JDK 1.8u25. Unit testing library is TestNG, version 6.8.13; Mockito is version 1.10.17.
在我的 GUI 应用程序中,JavaFX 所谓的控制器"是相当被动的,因为这个控制器"(我称之为显示器")真正做的唯一事情就是发送事件.
In my GUI application, what JavaFX calls a "controller" is pretty passive, in the sense that the only thing that this "controller" (which I call a "display") really does is send events.
现在,当接收到需要更新 GUI 的事件时,它是另一个类,我称之为视图,它负责更新 GUI.简而言之:
Now, when an event is received which requires a GUI update, it is another class, which I call a view, which is responsible for updating the GUI. In short:
显示 -> 演示者 -> 视图 -> 显示
display -> presenter -> view -> display
我对其中两个进行了单元测试:
I have unit tests for two of these:
- 显示 -> 演示者;
- 演示者 -> 视图.
所以,我在这方面几乎涵盖了(优势在于我可以更改显示,这就是我这样做的原因).
So, I am pretty much covered on this front (with the advantage that I can change the display, which is why I'm doing it that way).
但现在我尝试测试视图 -> 显示"部分;我是 SOL.
But now I try and test the "view -> display" part; and I am SOL.
作为说明,这里是视图类:
As an illustration, here is the view class:
匹配的显示类是这样的:
The matching display class is this:
这是我的测试文件:
我希望它能够工作......但它没有.然而相距甚远"我试图避开平台代码,即使上面的测试类也因这个异常而失败:
I expected it to work... Except that it doesn't. However "far apart" I try to steer away from the platform code, even the test class above fails with this exception:
那么,简而言之,我该如何防止上述异常的发生呢?我原以为将小部件模拟掉就足够了,但显然不是:/看起来我需要模拟整个平台上下文"(因为没有更好的词),但我不知道怎么做.
So, in short, how do I prevent the exception above from happening? I'd have thought that mocking the widgets away would have been enough, but apparently not :/ It looks like I need to mock the whole "platform context" (for lack of a better word for it) but I have no idea how.
推荐答案
好吧,第一件事:我从来没有用过一次 Mockito.但我很好奇,所以我花了几个小时来解决这个问题,我想还有很多需要改进的地方.
Ok, first things first: I never used Mockito once in a life. But I was curious, so I spent several hours to figure this out and I guess there is much to improve.
所以要让这个工作,我们需要:
So to get this working, we need:
- 上述(@jewelsea)JUnit 线程规则.
- 一个自定义的
MockMaker
实现,包装了默认的CglibMockMaker
. - 将所有东西连接在一起.
- The aforementioned (by @jewelsea) JUnit Threading Rule.
- A custom
MockMaker
implementation, wrapping the defaultCglibMockMaker
. - Wire the things together.
所以 1+2 是这样的:
So 1+2 is this:
数字 3 只是按照手册:
Number 3 is just following the manual:
- 复制我们的 MockMaker 的完全限定类名,例如.
org.awesome.mockito.JavaFXMockMaker
. - 创建一个文件mockito-extensions/org.mockito.plugins.MockMaker".该文件的内容正好是带有限定名称的一行.
快乐的测试和感谢 Andy Till 的线程规则.
Happy testing & kudos to Andy Till for his threading rule.
警告:这种实现对 MockMaker
使用 CglibMockMaker
可能不是你想要的(见 JavaDocs).
Warning: This implementation kind of hard-codes the MockMaker
to use the CglibMockMaker
which might not be be what you want in every case (see the JavaDocs).
这篇关于你如何模拟 JavaFX 工具包初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!