我正在为我班上的一个方法写一个测试类。我在班上没什么变化。下面是我的代码的简单版本。
Class AInternal extend A {
public List getList(listArg1, listArg2) {
boolean flag = isCorrectFlag();
if (flag) {
return getListForNext(nextArg1, nextArg2);
}
}
public boolean isCorrectFlag(){
//logic and return value
}
protected List getListForNext(nextArg1, nextArg2) {
AIteror arit = new AIterator();
String s = fetchValue();
arit.getObject();
}
protected class AIterator {
public Iterator getObject() {
//logic and return iterator
}
}
}
Class A {
public String fetchValue() {
//logic and return value
}
}
下面是我的测试方法
@Test
public void getList() throws Exception {
try {
AInternal AInternalSpy = spy(new AInternal());
AIterator AIteratorSpy = spy(AInternal.new AIterator());
Iterator<Object> iterator1 = ep.iterator();
doReturn(Boolean.TRUE).when(AInternalSpy).isCorrectFlag();
doReturn("value").when(AInternalSpy).fetchValue();
doReturn(iterator1).when(AIterator).getObject();
AInternalSpy.getIteratorPartitions(arg1, arg2);
}
catch (IllegalArgumentException e) {
throw new Exception(e);
}
}
前两个doreturn返回正确的值,但是getobject()方法从类中调用该方法,并且不返回存根值。我尝试使用powermockito.whennew返回模拟示例,但没有成功。任何提示都会有帮助。
1条答案
按热度按时间xam8gpfp1#
您可以将创建的新示例移动到一个新函数中,如下所示:
现在,可以将getlistfornext(…)函数修改为:
现在为新创建的getiteratorinstance()函数返回一个模拟或间谍的aiterator:
你的测试现在应该执行了。