带有异常的Java JUnit测试用例

时间:2013-05-14 20:46:27

标签: java unit-testing exception junit

这有什么不对。我有junit 4 enter image description here

2 个答案:

答案 0 :(得分:5)

您可以在@Test注释上声明,要使测试通过,必须抛出以下异常:

@Test(expected = NullPointerException.class)
public void testSynapseOne() {
    // test
}


@Test(expected = IllegalStateException.class)
public void testSynapseTwo() {
    // test
}

当然,你必须确保你正在测试正确的东西 - 目前,你的测试没有使用构造函数,这是你想要测试的关键部分。

哦 - 你不想让你的测试扩展TestCase unless you need compatibility with JUnit3.x

答案 1 :(得分:2)

您可以使用注释@Test(expected = TheClassException.class)编写一个应该抛出类TheClassException

的例外的测试
相关问题