测试类
public class CollectionImplementationUnitTest {
CollectionImplementation colImp;
public void setup() throws Exception {
...
colImp = Mockito.spy(new CollectionImplementation());
...
}
private String mockHistoryFromStrgyTable() {
String value1 = "myValue";
return value1;
}
@Test
public void testgetinfo (){
...
Mockito.when(
colImp.historyFromStrgyTable(
Mockito.anyString(),Mockito.anyString(),Mockito.anyString()
)
)
.thenReturn(mockHistoryFromStrgyTable());
CollectionsAccount Info = colImp.accountInfo(
"string1","string2","string3", new IdentityAcc(), TableLst
);
//sometestmethods and asserts
}
}
测试中的课程
public class CollectionImplementation {
...
@Override
public CollectionsAccount accountInfo(("string1","string2","string3", new IdentityAcc(), TableLst)) {
DetailsHelper helper = new (db2, "string",getmethod());
return helper.accountInfo("string1","string2", new IdentityAcc(), TableLst);
}
public String historyFromStrgyTable(){
//contains a call to the data base
}
}
DetailsHelper
public class DetailsHelper{
public CollectionsAccount accountInfo((String string1,String string2,String string3, new IdentityAcc(), TableLst)){
...
String paymentdetails = historyFromStrgyTable();
}
public String historyFromStrgyTable(){
//contains a call to the data base
}
}
当我尝试模拟HistoryFromStrgyTable()方法的数据时,实际上是调用HistoryFromStrgyTable()而不是从mockHistoryFromStrgyTable()获取。
我的测试用例在此行失败
Mockito.when(col_Imp.HistoryFromStrgyTable(Mockito.anyString(),
Mockito.anyString(),Mockito.anyString())).thenReturn( mockHistoryFromStrgyTable());
任何人都可以帮助我。我不明白什么是错的。我还将方法mockHistoryFromStrgyTable()从private更改为public,因为mockito无法模拟私有方法。
答案 0 :(得分:1)
这种情况正在发生,因为您正在使用间谍,而不是模拟。运行"真实"你调用它时的方法正是Mockito间谍应该做的事情。
要存储间谍,这是您要使用的语法。
Mockito.doReturn(mockHistoryFromStrgyTable()).when(colImp).
historyFromStrgyTable(Mockito.anyString(),Mockito.anyString(),Mockito.anyString());
您可以在my post here中找到更多详情。