Mockito-文件的模拟行为 [英] Mockito - Mocking behaviour of a File

查看:358
本文介绍了Mockito-文件的模拟行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以接收单个文件,查找与之相关的文件并打开它的类.类似于

class DummyFileClass
{
    private File fileOne;
    private File fileTwo;
    public DummyFileClass(File fileOne)
    {
         this.fileOne = fileOne;
         fileTwo = findRelatedFile(fileOne)
    }

    public void someMethod()
    {
         // Do something with files one and two
    }

}

在单元测试中,我希望能够测试someMethod()而不必将物理文件放在某处.我可以模拟fileOne并将其传递给构造函数,但是由于在构造函数中正在计算fileTwo,因此我对此无能为力.

我可以模拟方法findRelatedFile()-但这是最佳实践吗?在这里寻找最佳设计,而不是寻求实用的解决方法.我对模拟框架还很陌生.

解决方案

在这种情况下,我将使用物理文件来测试组件,而不依赖于模拟框架.正如fge所提到的那样,这可能会更容易,而且您不必担心您对模拟可能做出的任何错误假设.

例如,如果您依赖 您可能会让模拟返回固定的File列表,但是不能保证它们的返回顺序-只有在其他平台上运行代码时,您才可能发现这一事实. >

我会考虑使用JUnit的 TemporaryFolder 规则来帮助您设置测试所需的文件和目录结构,例如:

public class DummyFileClassTest {
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void someMethod() {
        // given
        final File file1 = folder.newFile("myfile1.txt");
        final File file2 = folder.newFile("myfile2.txt");

        ... etc...
    }
}

该规则应在测试完成时清除所有创建的文件和目录.

I have a class that takes in a single file, finds the file related to it, and opens it. Something along the lines of

class DummyFileClass
{
    private File fileOne;
    private File fileTwo;
    public DummyFileClass(File fileOne)
    {
         this.fileOne = fileOne;
         fileTwo = findRelatedFile(fileOne)
    }

    public void someMethod()
    {
         // Do something with files one and two
    }

}

In my unit test, I want to be able to to test someMethod() without having to have physical files sitting somewhere. I can mock fileOne, and pass it to the constructor, but since fileTwo is being calculated in the constructor, I don't have control of this.

I could mock the method findRelatedFile() - but is this the best practice? Looking for the best design rather than a pragmatic workaround here. I'm fairly new to mocking frameworks.

解决方案

In this sort of situation, I would use physical files for testing the component and not rely on a mocking framework. As fge mentions it may be easier plus you don't have to worry about any incorrect assumptions you may make of your mock.

For instance, if you rely upon File#listFiles() you may have your mock return a fixed list of Files, however, the order they are returned in is not guaranteed - a fact you may only discover when you run your code on a different platform.

I would consider using JUnit's TemporaryFolder rule to help you set up the file and directory structure you need for your test, e.g.:

public class DummyFileClassTest {
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void someMethod() {
        // given
        final File file1 = folder.newFile("myfile1.txt");
        final File file2 = folder.newFile("myfile2.txt");

        ... etc...
    }
}

The rule should clean up any created files and directories when the test completes.

这篇关于Mockito-文件的模拟行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆