gpt4 book ai didi

java - Jmock Mockery,模拟一个文件系统对象

转载 作者:行者123 更新时间:2023-11-30 07:34:54 26 4
gpt4 key购买 nike

我希望能够使用 Mockery 在 java 中模拟 File 对象。我似乎无法在 Java 中为文件创建接口(interface)。这可能吗?

编辑:

我需要测试 Indexer 类中的 indexDoc 函数。

@Test
public void testindexDocs()
{
final File f = mockFile.mock(File.class);
File file = new File("test");
mockFile.setImposteriser(ClassImposteriser.INSTANCE);
final String[] files = {
"C:\\test\\",
"C:\\test\\test1.html",
"C:\\test\\test2",
"C:\\test\\test3.html"};
mockFile.checking(new Expectations(){
{
one(f).list();will(returnValue(files));
}
});
//TODO test if list() how many time i have called
//Document doc = HTMLDocument.Document(file); in function indexDocs
}

Indexer类中的Index Docs函数

private static void indexDocs(File file) throws Exception{
//Check for file to be a directory or file to be indexed look for html files and add to document
if(file.isDirectory()){
String[] files = file.list();
Arrays.sort(files);
for (int i = 0; i < files.length; i++) // recursively index them
indexDocs(new File(file, files[i]));
} else if(file.getPath().endsWith(".html") || file.getPath().endsWith("htm")){
// Get the document from HTMLDocument class which takes care of stripping of HTML tag, get the path
// of HTML file and title of HTML document.
Document doc = HTMLDocument.Document(file);

// TODO Get the book of HTML, it can be a part of HTML document class.
writer.addDocument(doc);
}
}

最佳答案

不要模拟文件系统。我们在早期尝试这样做,但它使我们无法使用测试来指导设计。

快速查看您的代码,有两件事正在进行,一是文件导航,另一是 html 剥离。也许一种选择是引入一个 html 剥离对象(作为协作者传入)并模拟它,然后针对真实文件系统中的示例编写测试。

关于java - Jmock Mockery,模拟一个文件系统对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4900129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com