简体   繁体   English

在 JUnit 中,当 @Test 方法抛出未处理的异常时,@After 方法会运行吗?

[英]In JUnit, will @After method run, when @Test method throws an exception which is unhandled?

即使@Test 方法抛出未处理的异常或者这实际上是由 JUnit 内部完成的,我将如何确保我的 @After 方法运行?

JUnit runs methods annotated with @After after each test case regardless of thrown exceptions. JUnit 在每个测试用例之后运行带有@After注释的方法,而不管抛出的异常如何。

To quote the JUnit documentation :引用JUnit 文档

All @After methods are guaranteed to run even if a Before or Test method throws an exception.即使 Before 或 Test 方法抛出异常,也保证所有 @After 方法都会运行。

Yes, an @After method is always run, even if an exception is thrown in a @Test method.是的,@ @After方法始终运行,即使在@Test方法中抛出异常也是如此。

The test will fail if it isn't configured with @Test(expected=ExceptionClass.class) .如果未使用@Test(expected=ExceptionClass.class)配置,则测试将失败。

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

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