JUnit @Test expected annotation not working

前端 未结 8 1658
孤城傲影
孤城傲影 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 18:10

    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.

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

    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.

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