Java 使用 Mockito 的 Mock File、FileReader 和 BufferedReader 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18977057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:01:15  来源:igfitidea点击:

Mock File, FileReader and BufferedReader class using Mockito

javajunit4mockito

提问by user2654241

I got below code in a method which I want to test

我在要测试的方法中得到以下代码

File f = map.get("key1")
BuffereReader r = new BufferedReader(new FileReader(f));
String line=null;
do {
    line=r.readLine();
} while(r!=null);

I want to mock this operation so that I can pass the content of the file from the JUnit test case. I have done below:

我想模拟这个操作,以便我可以从 JUnit 测试用例中传递文件的内容。我在下面做了:

Map fles = Mockito.mock(ConcurrentHashMap.class);
File file = Mockito.mock(File.class);
Mockito.when(files.get("key1")).thenReturn(file);

FileReader fileReader = Mockito.mock(FileReader.class);
BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);

try {
     PowerMockito.whenNew(FileReader.class).withArguments(file).thenReturn(fileReader);
     PowerMockito.whenNew(BufferedReader.class).withArguments(fileReader).thenReturn(bufferedReader);
     PowerMockito.when(bufferedReader.readLine()).thenReturn("line1")
         .thenReturn("line2").thenReturn("line3");

    } catch (Exception e) {
        Assert.fail();
    }

So basically I need to pass "line1", "line2" and "line3" as lines from the file which are read by the mocked BufferedReader.

所以基本上我需要将“line1”、“line2”和“line3”作为文件中的行传递给模拟的BufferedReader。

However upon doing that, it failes as NullPointerException when trying to instantiate new FileReader(f) part.

然而,在这样做时,它在尝试实例化新的 FileReader(f) 部分时失败为 NullPointerException。

So is it because I can't mock a BufferedReader or the approach is wrong?

那么是因为我无法模拟 BufferedReader 还是方法错误?

Thanks

谢谢

回答by Sai

You can try PowerMock annotations. Check this : https://code.google.com/p/powermock/wiki/MockConstructor

您可以尝试 PowerMock 注释。检查这个:https: //code.google.com/p/powermock/wiki/MockConstructor

回答by Bobbo

You could extract a method for doing the actual reading and then test that instead. That would mean you only have to mock a single reader. For example, your code would be something like this:

您可以提取一种方法来进行实际阅读,然后对其进行测试。这意味着你只需要模拟一个读者。例如,您的代码将是这样的:

public void aMethod(){
    File f = map.get("key1")
    BuffereReader r = new BufferedReader(new FileReader(f));
    readStuff(r);
}

public void readStuff(BufferedReader r){
    String line=null;
    do {
        line=r.readLine();
    } while(r!=null);
}

then your test code would be more like the following:

那么您的测试代码将更类似于以下内容:

BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);
Mockito.when(bufferedReader.readLine()).thenReturn("line1", "line2", "line3");
objUnderTest.readStuff(bufferedReader);
//verify result