用EasyMock能Mock方法内New的对象吗
现在要做某个方法的Junit测试,异常不好测就想用EasyMock来实现。代码如下
要测的方法
public static boolean createFile(String strFileName, byte[] byteData) {
//这里省略关于文件名的一些正确性的判定
File newfile = new File(strFileName);
try {
FileOutputStream writefile = new FileOutputStream(strFileName);
try {
writefile.write(byteData);
} catch (IOException e1) {
return false;
}
} catch (FileNotFoundException e0) {
return false;
}
return true;
}
测试方法
@Test
public void testCreateFile008() {
String strFileName="//testfile//CreateFile.txt";
byte[] byteDate={0x17,0x18,0x19,0x20,0x21};
FileOutputStream mockFileOutputStream=EasyMock.createMock(FileOutputStream.class);
try{
mockFileOutputStream.write(byteDate);
EasyMock.expectLastCall().andThrow(new IOException());
EasyMock.replay(mockFileOutputStream);
}catch (Exception e){
System.out.print(e.toString());
}
boolean result=FileUtils.createFile(allPath, byteDate);
EasyMock.verify(mockFileOutputStream);
assert(result);
}
想Mock FileOutputStream的write方法抛出异常,但结果Mock的方法始终不执行,是因为这是静态方法吗?还是因为Mock的方法不能替代方法中New的类? 我想要Mock write方法抛出异常完成测试该怎么写啊?
求大神指教