JUnit的@Test标注预期不工作 [英] JUnit @Test expected annotation not working
问题描述
我已经得到了如下测试:
I've got the following test:
@Test(expected = IllegalStateException.class)
public void testKey() {
int key = 1;
this.finder(key);
}
但JUnit的报告,测试结果失败,尽管它抛出 - 预期 - 一个 IllegalStateException异常
我一定要配置别的东西,使这个运行?
Do I have to configure something else to make this run?
我现在运行测试以
@RunWith(Suite.class)
@SuiteClasses(Test.class)
public class TestSuite {
}
像<一个href=\"http://stackoverflow.com/questions/5536448/how-to-create-a-junit-4-test-suite-to-pass-when-expected-exceptions-are-thrown\">this问题,但我仍然没有得到预期的结果。
like in this question, but am still not getting the desired result.
而当我删除测试
preFIX我仍然得到一个错误。
And when I remove the test
prefix I'm still getting an error.
我得说,我运行这些测试与Eclipse,但它的配置为使用JUnit 4中,亚军。
I gotta say that I run these tests with Eclipse, but it's configured to use the JUnit 4 Runner.
推荐答案
的问题是,在该测试嵌套类是测试用例
的延伸。由于这是JUnit 3中的风格,注释没有工作。
The problem was, that the class in which the test was nested was an extension of TestCase
. Since this is JUnit 3 style, the annotation didn't work.
现在我的测试类是对自己的一类。
Now my test class is a class on its own.
这篇关于JUnit的@Test标注预期不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!