我是JUnit测试的新手,需要在测试异常方面得到一些提示。
我有一个简单的方法,如果输入字符串为空,则抛出异常:
public SumarniVzorec( String sumarniVzorec) throws IOException
{
if (sumarniVzorec == "")
{
IOException emptyString = new IOException("The input string is empty");
throw emptyString;
}
我希望测试当参数为空字符串时,异常是否被正确抛出。为此,我使用以下代码:
@Test(expected=IOException.class)
public void testEmptyString()
{
try
{
SumarniVzorec test = new SumarniVzorec( "");
}
catch (IOException e)
{ // Error
e.printStackTrace();
}
结果是抛出异常,但测试失败了。 我错过了什么吗? 谢谢,Tomas
throws IOException
。 - developmentalinsanity