我正在使用反射测试以下私有方法(getPrintWriter):
public abstract class CsvAuditor extends Auditor {
public void run() {
PrintWriter writer = getPrintWriter();
if (writer == null)
return;
writeReport(writer);
writer.close();
}
private PrintWriter getPrintWriter() {
PrintWriter writer;
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
Date date = new Date();
writer = new PrintWriter("report__" + dateFormat.format(date) + ".csv");
}
catch (FileNotFoundException ex) {
System.err.println(ex.getMessage());
return null;
}
return writer;
}
}
Run Code Online (Sandbox Code Playgroud)
我想模拟PrintWriter的构造函数抛出FileNotFoundException.
@RunWith(PowerMockRunner.class)
@PrepareForTest({CsvAuditor.class, PrintWriter.class})
public class CsvAuditorTest {
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@Before
public …Run Code Online (Sandbox Code Playgroud)