昨天学JUnit,今天学Mockito
我写了一个简单的类;
public class FileOperations {
public boolean autoMove(){
List<byte[]> patterns = getListofPatterns();
for(byte[] pattern: patterns){
System.out.println(new String(pattern));
if(seekInHeader(pattern)){
//logic to move file of specific folder of specific extension
return true;
}
}
return false;
}
public boolean seekInHeader(byte[] pattern){
return false;
}
public List<byte[]> getListofPatterns(){
return null;
}
}
并尝试按如下方式对其进行测试
@Test
public void autoMoveTest(){
FileOperations fo = mock(FileOperations.class);//stub
List<byte[]> dummyPatterns = new ArrayList<byte[]>();//specify stub value
dummyPatterns.add("amit".getBytes());
when(fo.getListofPatterns()).thenReturn(dummyPatterns);
when(fo.seekInHeader(anyString().getBytes())).thenReturn(true);
System.out.println(new String(fo.getListofPatterns().get(0)));
System.out.println(fo.seekInHeader("amit".getBytes()));
System.out.println(fo.autoMove());
assertTrue(fo.autoMove());
}
输出:
阿米特
真的
错误的
当我将 seekHeader() 设置为返回 true 时。为什么 fo.autoMove() 返回 false?