I’m beginner, and keep yourself in hands.
I have some easy program, and I need do junit test for write method. I have some collection in input. How I can do this? This my code:
// write to file
public void write(String fileName,
List<FigureGeneral> figuresList) {
try {
PrintWriter out = new PrintWriter(
new File(fileName).getAbsoluteFile());
try {
for (int i = 0; i < figuresList.size(); i++) {
out.println(figuresList.get(i).toString());
}
} finally {
out.close();
}
} catch (IOException e) {
System.out.println("Cannot write to file!");
}
}
And I want to know, coz after this I read from file, can we join both tests(write/read) or better do this individually(coz if our test fall we don’t know where is problem – in read or write)? How should make this correctly at junit(with prepare to test, and test itself)?
Better show on example, this way better to understand.
Thanks, Nazar.
I’ll suggest you making an interface wrapper around you IO classes (the
PrintWriterclass in your case) so you can use mock objects for output. You don’t have to test JavaPrintWriter, you want to test your functionality, right?So your class will be
The signature of the
MyWriterinterface is pretty straightforward.Then you can use EasyMock to write a test. The test method will be something like