I have a constructor that may throw an IOException
:
public MyClass(string url) throws IOException { ... }
Now I want to test of the exception is thrown in certain scenarios using a parameterized test. Can I annotate my test-method with a value for url
and the expected exception, something like this?
@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 supports Prameterized. Try this:
@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);
}
}