Jmock Mockery,模拟文件系统对象

发布于 2024-10-15 19:26:04 字数 1538 浏览 3 评论 0原文

我希望能够使用 Mockery 来模拟 java 中的 File 对象。我看来我们可能无法在java中为文件创建一个接口。这可能吗?

编辑:

我需要测试 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);
  }
}

I want to be able to mock the File object in java using Mockery. I seems like we may not be able to create an interface for the File in java. Is this possible?

EDIT:

I need to test the indexDoc function in Indexer Class.

@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  
}

Index Docs function in Indexer class

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);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜中书 2024-10-22 19:26:04

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

快速浏览一下您的代码,会发现有两件事发生,一是文件导航,二是 html 剥离。也许一种选择是引入一个 html 剥离对象(作为协作者传入)并模拟它,然后针对真实文件系统中的示例编写测试。

Don't mock the file system. We tried to do this in the early days and it diverted us from using tests to guide the design.

From a quick look at your code, there are two things going on, one is file navigation, the other is html stripping. Perhaps one option would be to introduce a html stripping object (passed in as a collaborator) and mock that, then write tests against examples in a real file system.

纵性 2024-10-22 19:26:04

Jmock 可以模拟具体类。只要做

Mockery context = new Mockery();

context.setImposteriser(ClassImposteriser.INSTANCE);

Jmock can mock concrete classes. Just do

Mockery context = new Mockery();

context.setImposteriser(ClassImposteriser.INSTANCE);
樱&纷飞 2024-10-22 19:26:04

您遇到的问题是确切的原因,应该使用抽象而不是具体的类。

The problems your having are the exact reason, one should use abstractions rather than concrete classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文