如何对依赖于其他方法的测试方法进行单元化

用户373201:

我有以下方法

public static File getInventoryFileFromProperties(){
    String filePath = getProperty(ConfigProperties.MY_INVENTORY_FILE);
    logger.debug("Looking for inventory file at {}", filePath);

    return new File(filePath);
}

我如何针对以下条件对此进行单元测试,属性文件中不存在ConfigProperties.MY_INVENTORY_FILE。

getProperty() // gets values from property file
ConfigProperties.MY_INVENTORY_FILE // is an enum of keys 
米克助记符:

使访问外部资源(例如文件系统)的代码的单元可测试的最佳方法是创建抽象层,例如:

public class FileAccessor {        
    public String getProperty(ConfigProperties property) {
        // property file access code goes here
    }

    public File createFile(String filePath) {
        return new File(filePath);
    }        
}

然后,可以通过依赖项的构造函数注入被测类重构为使用资源访问器

public class ContainingClass {

    private FileAccessor fileAccessor;

    // this constructor is accessible for the unit tests
    ContainingClass(FileAccessor fileAccessor) {
        this.fileAccessor = fileAccessor;
    }

    // this constructor is used by normal client code
    public ContainingClass() {
        this(new FileAccessor());
    }

    public File getInventoryFileFromProperties(){
        String filePath = fileAccessor.getProperty(ConfigProperties.MY_INVENTORY_FILE);
        return fileAccessor.createFile(filePath);
    }
}

最后,现在您可以模拟文件访问,因此单元测试变得更加简单。此测试使用Mockito模拟框架来模拟依赖关系,并且还可以与早期版本的JUnit一起使用:

import static org.mockito.Mockito.*;
import org.junit.Test;

public class ContainingClassTest {

    @Test
    public void getInventoryFileFromProperties_MY_INVENTORY_FILE_isMissing() {

        FileAccessor fileAccessor = mock(FileAccessor.class);

        // arrange the config file to return a null value for the property
        when(fileAccessor.getProperty(ConfigProperties.MY_INVENTORY_FILE)).thenReturn(null);

        // act; call the method
        new ContainingClass(fileAccessor).getInventoryFileFromProperties();

        // assert that the file-creating method was called with a null path
        verify(fileAccessor).createFile(isNull(String.class));
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何对依赖于springBoot applicationContext的单元测试方法进行单元测试?

如何对依赖于实例属性的实例方法进行单元测试?

如何为依赖于其他服务或数据库的服务编写单元测试

单元测试依赖于StreamReader读取文件的方法

使用Jest对依赖于HTTP调用的代码进行单元测试的最佳方法?

获取和设置依赖于其他字段的方法

如何创建依赖于其他两种显示方法的显示方法?

如何测试依赖于环境变量的Rust方法?

依赖于其他吸气剂的单元测试Vuex吸气剂

FsCheck:如何生成依赖于其他测试数据的测试数据?

当processElement依赖于广播的数据时,如何在flink中对BroadcastProcessFunction进行单元测试

如何对依赖于 redux props 的组件进行单元测试

如何对依赖于ActivatedRoute参数的组件进行单元测试?

如何对依赖于HttpContext.GetGlobalResourceObject的类进行单元测试?

如何对依赖于元素高度的组件进行单元测试(角度为5)?

如何对依赖于外部库“ <script src =“ http:// stripe [...]”的API进行单元测试

对 Flask 应用程序进行单元测试,但避免它调用其他方法

如何找出其他包依赖于特定包?

如何动态更新依赖于其他成员的成员?

如何执行依赖于其他行的SQL查询?

如何让变量依赖于类中的其他变量?

如何构建依赖于其他 observables 的 observables?

如何使变量依赖于其他变量?

测试依赖于其他副作用的React Hooks副作用(或其他测试)

依赖于其他属性的属性

使用RSpec测试依赖于外部Cassandra调用的方法

如何使用Excel或其他方法修改参数以进行测试?

如何使用依赖于已运行的TestSetup方法的NUnit测试用例?

如何在Flutter中对依赖于第三方包装的代码进行单元测试?