小编Lau*_*lex的帖子

如何使用mockito-inline模拟抛出IOException的构造函数?

我如何模拟下一行:

Workbook templateWorkBook = null;
try {
  templateWorkBook = new XSSFWorkbook(templateWithoutEvents);
} catch (IOException ex){
  log.error("Error creating workbook from file");
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试类似的事情,但不起作用。

try (
                MockedConstruction<XSSFWorkbook> mocked = Mockito.mockConstructionWithAnswer(XSSFWorkbook.class, invocation -> null)
                ) {
            doReturn("2021-09-30").when(stpConfigurationMock).getStartDate();
            **when(new XSSFWorkbook()).thenThrow(IOException.class);**
            Workbook workBook = fileService.generateReport(listItem, startDate, endDate);
        }
Run Code Online (Sandbox Code Playgroud)

我在运行测试时遇到异常:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?谢谢,

constructor exception mocking mockito-inline

2
推荐指数
1
解决办法
3642
查看次数

标签 统计

constructor ×1

exception ×1

mocking ×1

mockito-inline ×1