开发者

jUnit how to use @Test(expected=...)

I have this method:

public String getNum(int x) throws Exception
{
    if(x == 0) {
         throws new Exception("x cannot be 0");
    }
    ...
}

now in my JUnit test, I try this

@Test(expected=Exception.class)
public void testNum() {
     String x = getNum(0);
}

But Eclipse开发者_如何学编程 still wants to me to either add a throws declaration or surround with try catch for String x = getNum(0);. What did I do wrong? This test should pass since I expected an Exception when I pass in 0.


Just add a throws clause to the declaration of testNum?


Just add throws declaration which Java compiler requires for checked exceptions. In your case it is nothing more than declaration :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜