本文介绍了EasyMock和测试受保护的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用EasyMock来测试是否调用了受保护的方法,不确定这是否是最好的方法...但是鉴于以下内容,我如何才能知道在callMe()时didIgetCalled()实际上已被调用
Trying to use EasyMock to test if a protected method gets called, not sure if this is the best way to do it ... but given the below, how can I tell that didIgetCalled() actually was called when callMe() was called?
public Class testMe(){
public int callMe(){
if(true){
didIgetCalled();
}
return 1;
}
protected int didIgetCalled(){
return 2;
}
}
推荐答案
此是一种无需使用EasyMock即可测试该方法的方法,但是我的建议是:如果它不是公开的,请不要为其编写测试
This is a way you can test the method without using EasyMock, however my recommendation is that: If it's not public, don't write a test for it
Method method = testMe.class.getDeclaredMethod("didIgetCalled", new Class[]{});
method.setAccessible(true);
testMe testClass = new testMe();
int invoke = (Integer) method.invoke(testClass);
assertEquals(2,invoke);
我知道这不会完全解决您的问题,但这只是一个开始 :)
这篇关于EasyMock和测试受保护的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!