简体   繁体   English

即使在抛出异常后,junit测试中的测试异常也会失败

[英]Testing Exception in junit tests fails even after the exception is thrown

Hello I am unit testing a method using the junit @Rule annotation , 您好,我正在使用junit @Rule批注对方法进行单元测试,

@Rule 
public ExpectedException exception  = ExpectedException.none(); 

@Test
public void testException() {
  //do something that throws RuntimeException
  exception.expect(RuntimeException.class);
}

The test fails with the Runtime exception when it should actually pass expecting the exception. 当测试应实际通过预期的异常时,测试将失败并显示Runtime异常。 Is there anything else I need to do ? 我还有什么需要做的吗?

exception.expect(RunTimeException.class) must be called before the code that throws the exception is executed. 必须在执行引发异常的代码之前调用exception.expect(RunTimeException.class)

@Rule 
public ExpectedException exception  = ExpectedException.none(); 

@Test
public void testException() {
  exception.expect(RunTimeException.class);
  //do something that throws RunTimeException
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM