我有一些这样的代码:
class FooBar {
private String stateFoo;
public FooBar(String stateFoo){
this.stateFoo = stateFoo;
}
public void foo() {
FooInst f = FooInstFactory.createSomeFooInst(AnotherStaticFooConfig.getSomePath);
f.justCountMe();
}
}
我的目标是确保 f.justCountMe()
只执行一次。我知道如何用mockito 来做这件事。
我不知道如何将 FooInst
的模拟版本注入(inject) foo
方法中?这样我就可以计算调用次数了?
有可能这样做吗?
最佳答案
您需要使用 powermockito 和模拟静态方法。请参阅此处的说明如何执行此操作
关于java - 统计方法变量实例的方法调用次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29554731/