我在我的 junit 中得到了这段代码:
new NonStrictExpectations(mPersonEvaluator) {
{
invoke(mPersonEvaluator, "doEvaluatePerson", withAny(String.class), withAny(Integer.class), withAny(Integer.class));
result = doEvaluatePerson((String)any, (Integer)any, (Integer)any);
}
};
我想从我的私有(private)方法 doEvaluatePerson((String)any, (Integer)any, (Integer)any);
生成结果每次 mPersonEvaluator
的业务逻辑中调用 doEvaluatePerson 方法时。
调用工作正常,但结果仅在 junit
设置期间计算一次。结果为空。
我的问题是如何在 jmockit
中声明此类用例这样模拟就使用我的私有(private)方法?
提前致谢
斯特凡
最佳答案
好的,我找到了答案。
一种可能的解决方案是使用像这样的Delegator
:
result = new Delegate<PersonArt>() {
PersonArt delegator(String pShortName, Integer pOld, Integer pSize)
{
return doEvaluatePersonArt(pShortName, pOld, pSize);
}
};
工作得很好。
关于java - 在 jmockit 中调用自己的私有(private)方法来获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880434/