如何在Android unitTest/AndroidTest中测试文件IO

如何在Android unitTest/AndroidTest中测试文件IO,android,unit-testing,android-instrumentation,Android,Unit Testing,Android Instrumentation,我正在努力提高我的项目的代码覆盖率,因为我编写了一种方法,使用android开发者网站上的以下代码片段将文件写入androidinternalStorage String FILENAME = "hello_file"; String string = "hello world!"; File testFile = new File(context.getFilesDir(), FILENAME); FileOutputStream fos =new FileOutputStream(file)

我正在努力提高我的项目的代码覆盖率,因为我编写了一种方法,使用android开发者网站上的以下代码片段将文件写入android
internalStorage

String FILENAME = "hello_file";
String string = "hello world!";
File testFile = new File(context.getFilesDir(), FILENAME);
FileOutputStream fos =new FileOutputStream(file);
fos.write(string.getBytes());
fos.close();
我的想法是通过读取文件并与
helloworld
并查看它们是否匹配,以证明我的编写函数在单元测试/Android Instrumentation测试中有效。然而,由于以下原因,对我来说测试这一点并不是很简单

  • 我不知道单元测试方面(JVM)的文件路径
  • 也不是从Android仪器测试的角度 在Android中测试这种IO功能的最佳实践是什么?我是否应该关心文件是否已创建并放置?或者我应该简单地检查
    fos
    from是否不为空

    FileOutputStream fos =new FileOutputStream(file);
    

    请给我一些建议。谢谢

    我不会测试文件是否保存-这不是您的系统,Android AOSP应该进行测试以确保文件确实保存

    您要测试的是,您是否告诉Android保存您的文件。也许是这样的:

    String FILENAME = "hello_file";
    String string = "hello world!";
    File testFile = new File(context.getFilesDir(), FILENAME);
    FileOutputStream fos =new FileOutputStream(file);
    
    public void saveAndClose(String data, FileOutputStream fos) {
        fos.write(data.getBytes());
        fos.close();
    }
    
    然后,您的测试将用于FOS,并且是:

       FileOutputStream mockFos = Mockito.mock(FileOutputStream.class);
       String data = "ensure written";
    
       classUnderTest.saveAndClose(data, mockFos);
    
       verify(mockFos).write(data.getBytes());
    
    第二次测试:

       FileOutputStream mockFos = Mockito.mock(FileOutputStream.class);
       String data = "ensure closed";
    
       classUnderTest.saveAndClose(data, mockFos);
    
       verify(mockFos).close();
    

    我不会测试文件是否保存-这不是您的系统,Android AOSP应该进行测试以确保文件确实保存

    您要测试的是,您是否告诉Android保存您的文件。也许是这样的:

    String FILENAME = "hello_file";
    String string = "hello world!";
    File testFile = new File(context.getFilesDir(), FILENAME);
    FileOutputStream fos =new FileOutputStream(file);
    
    public void saveAndClose(String data, FileOutputStream fos) {
        fos.write(data.getBytes());
        fos.close();
    }
    
    然后,您的测试将用于FOS,并且是:

       FileOutputStream mockFos = Mockito.mock(FileOutputStream.class);
       String data = "ensure written";
    
       classUnderTest.saveAndClose(data, mockFos);
    
       verify(mockFos).write(data.getBytes());
    
    第二次测试:

       FileOutputStream mockFos = Mockito.mock(FileOutputStream.class);
       String data = "ensure closed";
    
       classUnderTest.saveAndClose(data, mockFos);
    
       verify(mockFos).close();
    

    检查这个(具有良好的相关信息)=>另外,这是一个(严格定义的)集成测试,因为您不仅测试对象及其属性和方法,还测试写入某种类型的存储。这可能意味着您将模拟实际的文件。另一个条目也谈到了这一点。祝你好运。检查这个(有很好的相关信息)=>另外,这是一个(严格定义的)集成测试,因为你不仅测试对象及其属性和方法,还测试写入某种类型的存储。这可能意味着您将模拟实际的文件。另一个条目也谈到了这一点。祝你好运。谢谢你的投入!在这种特殊情况下,我只需要验证调用(save/close)是否确实被我的方法调用,而不必担心之后会发生什么?现在是的。只要您已经手动测试了保存是否有效,就不需要用集成测试来束缚您的系统,现在就坚持单元测试。这里是一个更深入的阅读感谢的投入!在这种特殊情况下,我只需要验证调用(save/close)是否确实被我的方法调用,而不必担心之后会发生什么?现在是的。只要您已经手动测试了保存是否有效,就不需要用集成测试来束缚您的系统,现在就坚持单元测试。这里有一个更深入的阅读