JUnit @Test期望注释不起作用

时间:2023-01-18 09:08:38

I've got the following test:

我有以下测试:

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

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

但是JUnit报告说,测试失败了,尽管它抛出了 - 正如预期的那样 - 一个IllegalStateException。

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

我是否必须配置其他内容才能运行?


I run the test now with

我现在正在进行测试

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

}

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

就像在这个问题中,但我仍然没有得到理想的结果。

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

当我删除测试前缀时,我仍然会收到错误。

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

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

8 个解决方案

#1


46  

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.

问题是,测试嵌套的类是TestCase的扩展。由于这是JUnit 3样式,因此注释不起作用。

Now my test class is a class on its own.

现在我的测试类本身就是一个类。

#2


8  

@RunWith(JUnit4.class)
public class MyTestCaseBase extends TestCase 

I also had problems with @Test(expected = ...) annotation when I extended TestCase class in my base test. Using @RunWith(JUnit4.class) helped instantly (not an extremely elegant solution, I admit)

当我在基础测试中扩展TestCase类时,我也遇到了@Test(expected = ...)注释的问题。使用@RunWith(JUnit4.class)帮助即时(不是一个非常优雅的解决方案,我承认)

#3


7  

i tried this one, and work perfectly as expected.

我试过这个,并按预期完美地工作。

public class SampleClassTest {
    @Test(expected = ArithmeticException.class )
    public void lost(){
        this.lost(0);
    }
    private void lost(int i) throws ArithmeticException {
        System.out.println(3/i);
    }
}

also ensure that junit4 is added as dependancy, @ (annotations) are new feature in junit 4.

还确保将junit4作为依赖项添加,@(注释)是junit 4中的新功能。

#4


5  

I faced same issue, solution is simple "Don't extends TestCase class"

我遇到同样的问题,解决方案很简单“不要扩展TestCase类”

#5


3  

No, this JUnit test should work as it is - there is nothing more needed on this side.

不,这个JUnit测试应该可以正常工作 - 这方面没有什么需要了。

What makes you sure that the test throws an IllegalStateException? Is it possible that it gets wrapped into another exception of different type?

是什么让你确定测试会抛出IllegalStateException?是否有可能将其包装到另一种不同类型的异常中?

Please post the exact failure message from JUnit.

请从JUnit发布确切的失败消息。

As @duffymo suggested, it is easy to verify what (if any) exception the test really throws.

正如@duffymo建议的那样,很容易验证测试真正抛出的异常(如果有的话)。

#6


2  

This looks correct to me.

这对我来说是正确的。

Check your assumptions. Are you sure it throws the exception? If what you say is true, removing the expected from the annotation should make it fail.

检查你的假设。你确定它会抛出异常吗?如果您说的是真的,从注释中删除预期应该使它失败。

I'd be stepping through the code with a debugger to see what's going on. I'll assume you have an IDE that will do so, like IntelliJ.

我将使用调试器逐步完成代码,看看发生了什么。我假设你有一个IDE会这样做,比如IntelliJ。

#7


1  

Just tested this under JUnit4: this DO work, test completes successfully. Look if it is a IllegalSelectorException or such.

刚刚在JUnit4下进行了测试:这个DO工作,测试成功完成。看看它是否是IllegalSelectorException等。

#8


0  

I had the same problem I just changed my imports statements. I removed :

我遇到了同样的问题,我刚刚更改了我的import语句。我删除了 :

import org.junit.jupiter.api.Test;

import junit.framework.TestCase;

and added :

并补充说:

import org.junit.Test;

And it worked fine for me.

它对我来说很好。

#1


46  

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.

问题是,测试嵌套的类是TestCase的扩展。由于这是JUnit 3样式,因此注释不起作用。

Now my test class is a class on its own.

现在我的测试类本身就是一个类。

#2


8  

@RunWith(JUnit4.class)
public class MyTestCaseBase extends TestCase 

I also had problems with @Test(expected = ...) annotation when I extended TestCase class in my base test. Using @RunWith(JUnit4.class) helped instantly (not an extremely elegant solution, I admit)

当我在基础测试中扩展TestCase类时,我也遇到了@Test(expected = ...)注释的问题。使用@RunWith(JUnit4.class)帮助即时(不是一个非常优雅的解决方案,我承认)

#3


7  

i tried this one, and work perfectly as expected.

我试过这个,并按预期完美地工作。

public class SampleClassTest {
    @Test(expected = ArithmeticException.class )
    public void lost(){
        this.lost(0);
    }
    private void lost(int i) throws ArithmeticException {
        System.out.println(3/i);
    }
}

also ensure that junit4 is added as dependancy, @ (annotations) are new feature in junit 4.

还确保将junit4作为依赖项添加,@(注释)是junit 4中的新功能。

#4


5  

I faced same issue, solution is simple "Don't extends TestCase class"

我遇到同样的问题,解决方案很简单“不要扩展TestCase类”

#5


3  

No, this JUnit test should work as it is - there is nothing more needed on this side.

不,这个JUnit测试应该可以正常工作 - 这方面没有什么需要了。

What makes you sure that the test throws an IllegalStateException? Is it possible that it gets wrapped into another exception of different type?

是什么让你确定测试会抛出IllegalStateException?是否有可能将其包装到另一种不同类型的异常中?

Please post the exact failure message from JUnit.

请从JUnit发布确切的失败消息。

As @duffymo suggested, it is easy to verify what (if any) exception the test really throws.

正如@duffymo建议的那样,很容易验证测试真正抛出的异常(如果有的话)。

#6


2  

This looks correct to me.

这对我来说是正确的。

Check your assumptions. Are you sure it throws the exception? If what you say is true, removing the expected from the annotation should make it fail.

检查你的假设。你确定它会抛出异常吗?如果您说的是真的,从注释中删除预期应该使它失败。

I'd be stepping through the code with a debugger to see what's going on. I'll assume you have an IDE that will do so, like IntelliJ.

我将使用调试器逐步完成代码,看看发生了什么。我假设你有一个IDE会这样做,比如IntelliJ。

#7


1  

Just tested this under JUnit4: this DO work, test completes successfully. Look if it is a IllegalSelectorException or such.

刚刚在JUnit4下进行了测试:这个DO工作,测试成功完成。看看它是否是IllegalSelectorException等。

#8


0  

I had the same problem I just changed my imports statements. I removed :

我遇到了同样的问题,我刚刚更改了我的import语句。我删除了 :

import org.junit.jupiter.api.Test;

import junit.framework.TestCase;

and added :

并补充说:

import org.junit.Test;

And it worked fine for me.

它对我来说很好。