问题描述
我有一个将GenericFile作为输入参数的类读取数据并进行一些附加处理.我需要对其进行测试:
I have class that gets GenericFile as input argument reads data and does some additional processing. I need to test it:
public class RealCardParser {
public static final Logger l = LoggerFactory.getLogger(RealCardParser.class);
@Handler
public ArrayList<String> handle(GenericFile genericFile) throws IOException {
ArrayList<String> strings = new ArrayList<String>();
FileInputStream fstream = new FileInputStream((File) genericFile.getFile());
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = br.readLine();//skip header
while ((strLine = br.readLine()) != null) {
l.info("handling in parser: {}", strLine);
strings.add(strLine);
}
br.close();
return strings;
}
}
问题出在新的FileInputStream上.我可以模拟GenericFile,但这是没有用的,因为FileInputStream检查文件是否存在.我改变了班级,这样:
The issue is with new FileInputStream. I can mock GenericFile but it is useless cause FileInputStream checks if file exists. I changed my class so:
public class RealCardParser {
public static final Logger l = LoggerFactory.getLogger(RealCardParser.class);
protected BufferedReader getBufferedReader(GenericFile genericFile) throws FileNotFoundException {
FileInputStream fstream = new FileInputStream((File) genericFile.getFile());
DataInputStream in = new DataInputStream(fstream);
return new BufferedReader(new InputStreamReader(in));
}
@Handler
public ArrayList<String> handle(GenericFile genericFile) throws IOException {
ArrayList<String> strings = new ArrayList<String>();
BufferedReader br = getBufferedReader(genericFile);
String strLine = br.readLine();//skip header
while ((strLine = br.readLine()) != null) {
l.info("handling in parser: {}", strLine);
strings.add(strLine);
}
br.close();
return strings;
}
}
所以现在我可以重写方法getBufferedReader和测试方法处理程序:
So now I can override method getBufferedReader and test method handler:
@RunWith(MockitoJUnitRunner.class)
public class RealCardParserTest {
RealCardParser parser;
@Mock
GenericFile genericFile;
@Mock
BufferedReader bufferedReader;
@Mock
File file;
@Before
public void setUp() throws Exception {
parser = new RealCardParser() {
@Override
public BufferedReader getBufferedReader(GenericFile genericFile) throws FileNotFoundException {
return bufferedReader;
}
};
when(genericFile.getFile()).thenReturn(file);
when(bufferedReader.readLine()).thenReturn("header").thenReturn("1,2,3").thenReturn(null);
}
@Test
public void testParser() throws Exception {
parser.handle(genericFile);
//do some asserts
}
}
处理程序方法现在已包含测试,但是我仍然发现了导致cobertura问题的方法getBufferedReader.如何测试方法getBufferedReader或也许还有另一种解决方案?
Handler method now is covered with tests, but I still have uncovered method getBufferedReader that leads to cobertura problems.How to test method getBufferedReader or maybe there is another solution of the problem?
推荐答案
您可以使用PowerMockRunner和PowerMockito模拟FileInputStream.参见以下代码进行模拟-
You can mock FileInputStream by using PowerMockRunner and PowerMockito. See the below code for mocking-
@RunWith(PowerMockRunner.class)
@PrepareForTest({
FileInputStream.class
})
public class A{
@Test
public void testFileInputStream ()
throws Exception
{
final FileInputStream fileInputStreamMock = PowerMockito.mock(FileInputStream.class);
PowerMockito.whenNew(FileInputStream.class).withArguments(Matchers.anyString())
.thenReturn(fileInputStreamMock);
//Call the actual method containing the new constructor of FileInputStream
}
}
这篇关于如何模拟FileInputStream和其他* Streams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!