JUnit @Test预计注释不起作用

问题描述:

我有以下测试:

@Test(expected = IllegalStateException.class)
public void testKey() {
    int key = 1;
    this.finder(key);
}

但是JUnit报告说,测试失败了,尽管如预期那样 - 一个 IllegalStateException

But JUnit reports, that the test fails, although it throws — as expected — an IllegalStateException.

我必须配置其他的东西才能运行吗?

Do I have to configure something else to make this run?

我现在使用

@RunWith(Suite.class)
@SuiteClasses(Test.class)
public class TestSuite {

}

喜欢在,但仍然没有得到期望的结果。

like in this question, but am still not getting the desired result.

当我删除 test 前缀我还在收到错误。

And when I remove the test prefix I'm still getting an error.

我不得不说,我用Eclipse运行这些测试,但是配置为使用JUnit 4 Runner。

I gotta say that I run these tests with Eclipse, but it's configured to use the JUnit 4 Runner.

问题是,测试嵌套的类是 TestCase 的扩展名。因为这是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.