问题描述
我在下面的代码中找到了我要测试的方法
文件f = map.get(key1)
BuffereReader r = new BufferedReader(new FileReader(f));
String line = null;
do {
line = r.readLine();
} while(r!= null);
我想模拟这个操作,以便我可以从JUnit测试用例中传递文件的内容。我在下面做了:
Map fles = Mockito.mock(ConcurrentHashMap.class);
文件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(例外e){
Assert.fail();
}
所以基本上我需要传递line1,line2和line3 作为来自文件的行,由模拟的BufferedReader读取。
但是,在尝试实例化新的FileReader(f)部分时,它会因NullPointerException而失效。 / p>
所以是因为我无法模拟BufferedReader或方法是错误的?
谢谢
您可以提取实际读数的方法,然后进行测试。这意味着你只需要嘲笑一个读者。例如,您的代码将是这样的:
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);
}
然后您的测试代码将更像以下内容:
BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);
Mockito.when(bufferedReader.readLine())。thenReturn(line1,line2,line3);
objUnderTest.readStuff(bufferedReader);
//验证结果
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:
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.
However upon doing that, it failes as NullPointerException when trying to instantiate new FileReader(f) part.
So is it because I can't mock a BufferedReader or the approach is wrong?
Thanks
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
这篇关于使用Mockito的Mock File,FileReader和BufferedReader类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!