Junit4 - 测试超时清理
当我使用注释 @Test(timeout = 3000) 运行测试并且超时时,它会立即终止测试,并且不会调用使用 @After 注释的tearDown 方法。
在这种情况下,清理的方法是什么?
编辑: 我的测试是使用 jax-rs 通过线路调用资源端点,并且测试在 http 请求中间超时。在这种情况下,我相当确定 @After 没有被调用
When I run a test with annotation @Test(timeout = 3000) and it times out, it kills the test immediately and does not call my tearDown method annotated with @After.
What would be the way to clean up in such a scenario?
EDIT:
My test is invoking resource end points using jax-rs over the wire and the test times out in the middle of a http request. This is the case I am fairly certain that @After is not being invoked
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
严格来说,它不会杀死测试,而是会失败。这显然意味着用
@After
注释的方法将运行。下面的代码对我来说就像魅力一样。
输出是,
Strictly speaking, it doesn't kill the test, it fails it. Which apparently means that the method annotated with
@After
will run.The code below is working like charm for me.
Output is,
我也遇到了超时属性的一些问题。也许这可以帮助您找到四个问题...
在我的例子中,混乱是由于用 @Test(timeout=...) 注释的方法中的测试代码在单独的线程中运行这一事实引起的。因此,我在测试期间访问的一些 ThreadLocal 内容无法在 @After 方法中清理。
I had some problems with the timeout attribute, too. Maybe this helps you in finding four problem...
In my case the confusion was caused by the fact that the test code in a method annotated with @Test(timeout=...) is run in a separate thread. Therefore some ThreadLocal stuff I accessed during the test could not be cleaned up in the @After method.
这适用于 JUnit 4.7。
This works from JUnit 4.7.