用JMockit和Kotlin捕获对象
我已经使用了JMockit很久了,我非常喜欢它。 但是,我遇到了一个我似乎无法解决的问题。 查看下面的一些Kotlin测试代码片段,测试Kotlin生产代码。
@Injectable lateinit var experimentStorage: ExperimentStorage ... val experimentCaptor = mutableListOf<Experiment>() object : Verifications() { init { experimentStorage.save(withCapture(experimentCaptor)) } }
当我运行我的测试时,我得到以下错误:
java.lang.IllegalStateException:withCapture(experimentCaptor)不能为null
我100%确定我的生产代码正确地执行存储,因为当我替换下面的捕获时,我的测试成功:
object : Verifications() { init { experimentStorage.save(withAny(experiment)) } }
有没有人有经验捕捉Kotlin与JMockit(1.28)的参数? 我究竟做错了什么? 我想它与init
块有关,因为在Java中你将使用静态空间…
最终,我无法在Kotlin找到解决这个问题的任何方法。 问题在于静态空间。 在Kotlin你有init
块,你必须记录你的Expectations
/ Verifications
,但JMockit实际上期望在静态空间(因此{{...}}
符号)。
我现在的解决方法是保持在Java中的捕获器,所以我有一个Captors
类在我的Java测试来源,看起来像这样
public class Captors { public static List<Experiment> experimentStorage_save(ExperimentStorage experimentStorage) { final List<Experiment> captor = new ArrayList<>(); new Verifications() {{ experimentStorage.save(withCapture(captor)); }}; return captor; } ... }