如何解决在powermockito中如何模拟具有另一个模拟对象作为参数的对象?
我有这些方法
public File getFileDetails(File file){
FileDetailMaker fileDetailMaker = new FileDetailMaker(getFileNumber(File.getName()));
return fileDetailMaker;
}
public String getFileNumber(String fileName){
return fileName.substring(0,fileName.indexOf(".")).substring(0,8);
}
我正在编写一个测试用例来测试 getFileDetails() 方法。如果我通过创建一个模拟“文件”对象并将“文件”的模拟作为参数发送来测试它,我会收到一个空指针异常,因为无法从模拟文件中获取子字符串。
我还通过
模拟了 FileDetailMaker 对象的创建FileDetailMaker fileDetailMaker = Mockito.mock(FileDetailMaker.class);
PowerMockito.whenNew(FileDetailMaker.class).withAnyArguments().thenReturn(fileDetailMaker);
但我仍然收到空指针异常。所以你能帮我提出这个问题的建议吗
解决方法
将此添加到您的测试用例
PowerMockito.when(file.getName()).thenReturn("filenameisthis.extension");
其中文件是文件类型的模拟对象
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。