我还在学习junit,我想知道如何为这个问题编写junit测试用例。我也使用 emma 插件来运行覆盖范围。将值设置为(字符串)路径和名称后该怎么办?在@Test
public static void createReport(final String path, final String name) throws IOException {
File outDir = new File("Out");
if (!outDir.exists()) {
if (!outDir.mkdir()) {
}
}
}
设置参数值后还需要使用assertEquals吗?
最佳答案
如果您使用 outDir.mkdirs()
(如果文件夹不存在,则会创建文件夹),那么 Emma 不会提示该行未被测试覆盖。
如果您想非常彻底,测试代码的方法是在故意丢失的目录下运行它并检查它是否已创建。作为测试的一部分删除输出文件夹:
File outDir = new File("Out")
/* You will probably need something more complicated than
* this (to delete the directory's contents first). I'd
* suggest using FileUtils.deleteDirectory(dir) from
* Apache Commons-IO.
*/
outDir.delete();
// Prove that it's not there
assertFalse(outDir.exists());
createReport(...);
// Prove that it has been created
assertTrue(outDir.exists());
或者将报告写入临时文件夹(如果您可以使用该选项)。
关于java - 文件的 Junit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19679228/