JUnit @Test expected annotation not working

前端 未结 8 1657
孤城傲影
孤城傲影 2020-12-05 17:46

I\'ve got the following test:

@Test(expected = IllegalStateException.class)
public void testKey() {
    int key = 1;
    this.finder(key);
}
<
相关标签:
8条回答
  • 2020-12-05 17:49

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

    import org.junit.jupiter.api.Test;
    
    import junit.framework.TestCase;
    

    and added :

    import org.junit.Test;
    

    And it worked fine for me.

    0 讨论(0)
  • 2020-12-05 17:50

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

    0 讨论(0)
  • 2020-12-05 17:56

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

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

    Please post the exact failure message from JUnit.

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

    0 讨论(0)
  • 2020-12-05 17:59
    @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)

    0 讨论(0)
  • 2020-12-05 18:04

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

    0 讨论(0)
  • 2020-12-05 18:08

    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.

    0 讨论(0)
提交回复
热议问题