小编Ele*_*eid的帖子

PowerMockito如何在构造函数调用上抛出异常

我正在使用反射测试以下私有方法(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)

java throw powermockito

2
推荐指数
1
解决办法
4658
查看次数

标签 统计

java ×1

powermockito ×1

throw ×1