java mock 模拟数据_Java中的模拟文件 – Mock内容 – Mockito

我很嘲笑,我一直在试图嘲笑实际内容(本质上是在内存中创建一个虚拟文件),以便任何时候都不会将数据写入磁盘。

我已经尝试过像嘲笑文件一样的解决方案,并嘲笑尽可能多的属性,然后还可以用文件撰写者/缓冲写作人员编写这些属性,但是这些属性无法正常工作,因为它们需要规范化路径。任何人找到一个这个或者类似的解决方案,但是我正在接近这个错误?

我一直这样做:

private void mocking(){

File badHTML = mock(File.class);

//setting the properties of badHTML

when(badHTML.canExecute()).thenReturn(Boolean.FALSE);

when(badHTML.canRead()).thenReturn(Boolean.TRUE);

when(badHTML.canWrite()).thenReturn(Boolean.TRUE);

when(badHTML.compareTo(badHTML)).thenReturn(Integer.SIZE);

when(badHTML.delete()).thenReturn(Boolean.FALSE);

when(badHTML.getFreeSpace()).thenReturn(0l);

when(badHTML.getName()).thenReturn("bad.html");

when(badHTML.getParent()).thenReturn(null);

when(badHTML.getPath()).thenReturn("bad.html");

when(badHTML.getParentFile()).thenReturn(null);

when(badHTML.getTotalSpace()).thenReturn(0l);

when(badHTML.isAbsolute()).thenReturn(Boolean.FALSE);

when(badHTML.isDirectory()).thenReturn(Boolean.FALSE);

when(badHTML.isFile()).thenReturn(Boolean.TRUE);

when(badHTML.isHidden()).thenReturn(Boolean.FALSE);

when(badHTML.lastModified()).thenReturn(System.currentTimeMillis());

when(badHTML.mkdir()).thenReturn(Boolean.FALSE);

when(badHTML.mkdirs()).thenReturn(Boolean.FALSE);

when(badHTML.setReadOnly()).thenReturn(Boolean.FALSE);

when(badHTML.setExecutable(true)).thenReturn(Boolean.FALSE);

when(badHTML.setExecutable(false)).thenReturn(Boolean.TRUE);

when(badHTML.setReadOnly()).thenReturn(Boolean.FALSE);

try {

BufferedWriter bw = new BufferedWriter(new FileWriter(badHTML));

/*

badHTMLText is a string with the contents i want to put into the file,

can be just about whatever you want

*/

bw.append(badHTMLText);

bw.close();

} catch (IOException ex) {

System.err.println(ex);

}

}

任何想法或指导将是非常有益的。

在此之后,我基本上尝试从使用另一个类的文件中读取。我会尝试模拟某种输入流,但是另一个类不需要输入流,因为它是项目的io处理类。


版权声明:本文为weixin_28716723原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。