无法使用 junit 测试异常
我有一个包含 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")
只要我只运行特定的测试用例,它就可以工作,但当我在测试套件中运行它时,它就不起作用。
I have a method which contains a try-catch block and I don't know how to make my test pass...
Here is my code:
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();
}
}
The only thing I want to do when the exception is thrown is to log it, and that all works great, however, how can I create a test that shows that it works?
I tried to capture console output and
assertTrue(consoleOutput.contains("logMessgae")
and that worked as long as I ran only that specific test case, but it did not work when I ran it in my test suite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该方法的约定是捕获 CustomException 并将“错误消息”记录到外部组件:记录器。因此,您应该创建一个模拟记录器,将其提供给 ClassToTest 实例,测试该方法并验证是否已使用正确的参数调用模拟记录器。
我个人使用 EasyMock (及其“classextension”扩展)来模拟具体类,但还有其他模拟框架。阅读 http://easymock.org/EasyMock3_0_Documentation.html 了解有关 EasyMock 和模拟的一般信息。
The contract of the method is to catch CustomException and log "Error Message" to an external component : the logger. You should thus create a mock logger, give it to the ClassToTest instance, test the method and verify that the mock logger has been called with the aright arguments.
I personnally use EasyMock (and its "classextension" extension) to mock concrete classes, but there are other mock frameworks. Read http://easymock.org/EasyMock3_0_Documentation.html for information about EasyMock and mocking in general.
如果没有适当的模拟文件,您的代码将无法测试。您可以通过模拟外部依赖项并使用依赖项注入(即通过 guice)。
Your code is untestable without having a mock file in place. You can make this code more testable by mocking out external dependencies and using dependency injection (i.e. via guice).
如果您尝试测试在某些条件下是否引发 CustomException,那么您应该测试引发异常的代码,而不是捕获异常的代码。在您的示例中,我猜测您未定义的“fileLoader”是您期望抛出异常的内容?
如果您尝试测试异常是否被捕获并且事情继续顺利运行,那么您需要重新设计 ClassToTest 以便可以注入潜在异常抛出类的模拟实现。该模拟实现总是会抛出异常。然后,您可以在测试中运行 ClassToTest 并断言任何表明它正确处理异常的条件。
If you're trying to test that CustomException is thrown under certain conditions, then you should be testing the code that throws the exception, rather than the code that catches it. In your example, I'm guessing that your undefined "fileLoader" is what you're expecting to throw the exception?
If you're trying to test that the exception is caught and things continue to run smoothly, then you need to redesign ClassToTest so that a mock implementation of the potentially exception throwing class can be injected. That mock implementation would always throw the exception. Then you can run through ClassToTest in your test and assert whatever condition indicates that it handled the exception properly.
我认为正确的方法是将记录器传递到类中,如果您使用像 Mockito 这样的模拟框架,则可以使用 验证:
这里的关键是传递您的依赖项,以便您可以验证与它们的交互。
I think the right way to do this is pass your logger into your class, and if you're using a mocking framework like Mockito, you can verify your logger with verify:
the key here is passing in your dependencies so you can then verify interactions with them.