Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试在使用mockito引发运行时异常时是否引发自定义异常_Java_Unit Testing_Exception_Junit_Mockito - Fatal编程技术网

Java 测试在使用mockito引发运行时异常时是否引发自定义异常

Java 测试在使用mockito引发运行时异常时是否引发自定义异常,java,unit-testing,exception,junit,mockito,Java,Unit Testing,Exception,Junit,Mockito,我有一段代码,捕获一些异常并抛出一个自定义异常 @Override public void config() throws CustomException{ File jsonFile = new File("config.json"); try { ConfigMapper config = mapper.readValue(jsonFile, ConfigMapper.class); try { this.instan

我有一段代码,捕获一些异常并抛出一个自定义异常

@Override
public void config() throws CustomException{
    File jsonFile = new File("config.json");
    try {
        ConfigMapper config = mapper.readValue(jsonFile, ConfigMapper.class);

        try {
            this.instanceId = Integer.parseInt(config.getConfig().getClientId());
            this.configParams = config.getConfig().getConfigParams();

        } catch (NumberFormatException ex) {
            throw new CustomException("Please provide a valid integer for instance ID", ex);
            //LOGGER.log(Level.SEVERE, "error initializing instanceId. Should be an integer " + e);
        }
    } catch (IOException ex) {
        throw new CustomException("Error trying to read/write", ex);
        // LOGGER.log(Level.SEVERE, "IOException while processing the received init config params", e);
    }
}
我需要为此编写一个单元测试,下面是我如何编写的

 @Test
public void should_throw_exception_when_invalid_integer_is_given_for_instanceID(){
    boolean isExceptionThrown = false;
    try{
        Mockito.doThrow(new NumberFormatException()).when(objectMock).config();
        barcodeScannerServiceMock.config();
    } catch (CustomException ex) {
        isExceptionThrown = true;
    }
    assertTrue(isExceptionThrown);
}
但是它抛出了一个数字格式异常,而不是我想要的CustomException。但这是有意义的,因为我正在使用mock对象抛出异常,因此我的代码逻辑不会执行。但是如果是这样的话,我该如何测试这个场景呢?请注意。

1。)删除行
Mockito.doThrow(newnumberformatexception()).when(objectMock.config()

2.)将JSON文件中的客户端ID更改为无法转换为整数的内容

this.instanceId=Integer.parseInt(config.getConfig().getClientId())将因此失败,并因此引发异常

关于名称的一个建议是:测试方法的名称应该是Java文档中编写的名称。只需将其命名为“testCustomException”&解释Java文档中的方法函数。Java中有命名约定(单击),它们基本上是通用的指导原则


练习这些方法非常有帮助,因为它可以让您在一个月左右没有使用代码后,快速重新进入代码。因为可读性提高了。

我完全不同意您关于命名的建议-
testCustomException
什么都不告诉您。OP目前的名字几乎就是spoton@MrWiggles您知道Java有命名约定,对吗?强烈建议不仅使用简短、清晰的名称来表示该方法的目的,而且在生词中使用大写字母,而不是下划线。你知道我写了一本关于良好单元测试实践的书,对吗?@Seth我们不应该在不修改代码的情况下测试代码吗?当前,instanceId是一个私有变量。所以我不能仅仅为了测试目的而改变它并调用该方法,对吗?有没有别的办法?使用模拟或其他功能?@MrWiggles我们难道不能在不进行更改的情况下测试代码吗?当前,instanceId是一个私有变量。所以我不能仅仅为了测试目的而改变它并调用该方法,对吗?有没有别的办法?使用模拟或其他功能?