参数化测试以检查构造函数是否抛出异常
parameterized test to check if constructor throws exception
我有一个构造函数可能会抛出 IOException
:
public MyClass(string url) throws IOException { ... }
现在我想使用参数化测试来测试在某些情况下抛出的异常。我可以用 url
的值和预期的异常来注释我的测试方法吗?
@Test("https://myHost/not.existsing", expected = IOException.class)
@Test("https://myHost/whrong.fileextension", expected = IOException.class)
public void MyTest(String url)
{
Assert.Throws(expected);
}
Junit 4 支持 Prameterized。试试这个:
@RunWith(Parameterized.class)
public class Test {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "https://myHost/whrong.fileextension" },
{ "https://myHost/not.existsing"}
});
}
private String url;
public Test(String url) {
this.url = url;
}
@Test(expected = IOException.class)
public void test() throws IOException {
MyClass myClass = new MyClass(url);
}
}
我有一个构造函数可能会抛出 IOException
:
public MyClass(string url) throws IOException { ... }
现在我想使用参数化测试来测试在某些情况下抛出的异常。我可以用 url
的值和预期的异常来注释我的测试方法吗?
@Test("https://myHost/not.existsing", expected = IOException.class)
@Test("https://myHost/whrong.fileextension", expected = IOException.class)
public void MyTest(String url)
{
Assert.Throws(expected);
}
Junit 4 支持 Prameterized。试试这个:
@RunWith(Parameterized.class)
public class Test {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "https://myHost/whrong.fileextension" },
{ "https://myHost/not.existsing"}
});
}
private String url;
public Test(String url) {
this.url = url;
}
@Test(expected = IOException.class)
public void test() throws IOException {
MyClass myClass = new MyClass(url);
}
}