java 如何为 IOException 编写 junit 测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36429274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write junit test cases for IOException
提问by user6090970
I want to check the IOException class in JUNIT testing. Here is my code:
我想在 JUNIT 测试中检查 IOException 类。这是我的代码:
public void loadProperties(String path) throws IOException {
InputStream in = this.getClass().getResourceAsStream(path);
Properties properties = new Properties();
properties.load(in);
this.foo = properties.getProperty("foo");
this.foo1 = properties.getProperty("foo1");
}
when I try to give false properties file path it gives NullPointerException. I want to get IOException and Junit test for it. Thank you very much for your help.
当我尝试给出错误的属性文件路径时,它给出了 NullPointerException。我想对其进行 IOException 和 Junit 测试。非常感谢您的帮助。
回答by DwB
Try this
试试这个
public TestSomeClass
{
private SomeClass classToTest; // The type is the type that you are unit testing.
@Rule
public ExpectedException expectedException = ExpectedException.none();
// This sets the rule to expect no exception by default. change it in
// test methods where you expect an exception (see the @Test below).
@Test
public void testxyz()
{
expectedException.expect(IOException.class);
classToTest.loadProperties("blammy");
}
@Before
public void preTestSetup()
{
classToTest = new SomeClass(); // initialize the classToTest
// variable before each test.
}
}
Some reading: jUnit 4 Rule- scroll down to the "ExpectedException Rules" section.
一些阅读: jUnit 4 Rule- 向下滚动到“ExpectedException Rules”部分。
回答by jjd
Check thisanswer. In short: you can mock the resource that you want to throw an exception and throw the exception by the mock in your tests. Mockito framework might help you with that. The details are under the link I have provided earlier
检查这个答案。简而言之:您可以模拟要抛出异常的资源,并在测试中通过模拟抛出异常。Mockito 框架可能会帮助你解决这个问题。详细信息在我之前提供的链接下
回答by uniknow
Not sure how we simulate the IOException
with the current implementation but if you refactor the code in something like this:
不确定我们如何IOException
用当前的实现来模拟,但如果你像这样重构代码:
public void loadProperties(String path) throws IOException {
InputStream in = this.getClass().getResourceAsStream(path);
loadProperties(in);
}
public void loadProperties(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
this.foo = properties.getProperty("foo");
this.foo1 = properties.getProperty("foo1");
}
And create a mocked InputStream, something like this:
并创建一个模拟的 InputStream,如下所示:
package org.uniknow.test;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
public class TestLoadProperties {
@test(expected="IOException.class")
public void testReadProperties() throws IOException {
InputStream in = createMock(InputStream.class);
expect(in.read()).andThrow(IOException.class);
replay(in);
// Instantiate instance in which properties are loaded
x.loadProperties(in);
}
}
WARNING:Created above code on the fly without verifying it by compiling so there might be syntax faults.
警告:在没有通过编译验证的情况下即时创建上面的代码,因此可能存在语法错误。