【发布时间】:2011-02-11 18:52:32
【问题描述】:
我有一个包含 try-catch 块的方法,但我不知道如何让我的测试通过...
这是我的代码:
public class ClassToTest {
public void loadFileContent() {
try {
InputStream fileStream = fileLoader.loadFileContent();
fileContentReader = new BufferedReader(new InputStreamReader(fileStream));
} catch(CustomException ce) {
logger.log(Level.SEVERE, "Error message")
}
}
}
public class TestClassToTest {
@Test
(expected = CustomException.class)
public void testCustomException() throws Exception {
loadFileContent();
}
}
当抛出异常时,我唯一想做的就是记录它,一切都很好,但是,我如何创建一个显示它有效的测试?
我试图捕获控制台输出和
assertTrue(consoleOutput.contains("logMessgae")
只要我只运行那个特定的测试用例,它就可以工作,但是当我在我的测试套件中运行它时它就不起作用了。
【问题讨论】:
标签: java exception testing junit