如何在 Roboelectric 中模拟 Scanner 扫描仪 = new Scanner(file, "UTF-8")

问题描述

我在使用 Roboelectric 或 Powermockito 模拟下面提到的行时遇到了问题

File file = new File(context.getFilesDir() + FILE_NAME);
Scanner scanner = new Scanner(file,"UTF-8")

它向我抛出以下异常 java.io.FileNotFoundException: "data/data/Logs/Text.txt" (系统找不到指定的文件)

即使我能够模拟文件并设置文件的路径。

关于如何模拟扫描仪类的任何建议。

解决方法

得到了解决方案,我刚刚在资源文件夹中创建了具有相同文件名的文件,并向其中添加了虚拟文本。这样当在测试用例中执行扫描程序语句时,它会读取虚拟文件。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...