尝试使用 EasyMock 来测试 protected 方法是否被调用,不确定这是否是最好的方法...但是考虑到以下内容,我如何知道在 callMe() 时确实调用了 didIgetCalled()被叫了?
public Class testMe(){
public int callMe(){
if(true){
didIgetCalled();
}
return 1;
}
protected int didIgetCalled(){
return 2;
}
}
最佳答案
这是一种无需使用 EasyMock 即可测试方法的方法,但我的建议是:如果它不是公开的,请勿为其编写测试
Method method = testMe.class.getDeclaredMethod("didIgetCalled", new Class[]{});
method.setAccessible(true);
testMe testClass = new testMe();
int invoke = (Integer) method.invoke(testClass);
assertEquals(2,invoke);
我知道这并不能完全解决您的问题,但这是一个开始:)
关于java - EasyMock 和测试 protected 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9122339/