单元测试用例:针对特定场景模拟或调用真实方法

时间:2019-10-17 17:29:14

标签: java junit mockito powermock

我有一种检查可用空间的方法。如果超过阈值,则记录日志,生成线程转储,然后退出。

我不确定如何为此编写测试用例。

下面是执行上述任务的方法:

void watchDog() {
    FileStore fileStore = null;
    long total = 0;
    long free = 0;
    double percent_free = 0;
    try {
        fileStore = Files.getFileStore(Paths.get(dirToWatch));
        total = fileStore.getTotalSpace();
        free = fileStore.getUsableSpace();

        percent_free = 100.0 * ((double) (total - free) / (double) total);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (percent_free >= thresholdValue) {
        StringBuilder str = new StringBuilder();
        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
        for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
            str.append(ti);
        }
        LOGGER.error(str.toString());
        System.exit(0); // Exit after dumping and logging Thread dumps
    }
}

我不知道如何对这段代码进行单元测试?对该方法进行单元测试需要什么策略?我是否需要在单元测试中实际执行此代码,否则模拟将有所帮助?

重要的一点是,System.exit()使用此方法是否会终止Junit本身?

任何准则都会有很大帮助。

0 个答案:

没有答案
相关问题