包装 JUnit 测试(在 Eclipse 中)
我对 Groovy 代码的所有测试看起来都是这样,
public void testButtons() {
try {
page.getButtons();
} catch (Exception e) {
throw org.codehaus.groovy.runtime.StackTraceUtils.sanitize(e);
}
}
因为我需要清理任何可能出现的 StackTrace(否则很难阅读,因为它包含所有 Groovy 元代码)。有没有办法指定所有 JUnit 测试都以特定方式包装(如错误处理程序)?
注意:我在 Eclipse 中运行这些,但如果有一种方法可以在 IntelliJ 或 Netbeans 中执行此操作,那么了解一下就好了。
All of my tests for my Groovy code look like this
public void testButtons() {
try {
page.getButtons();
} catch (Exception e) {
throw org.codehaus.groovy.runtime.StackTraceUtils.sanitize(e);
}
}
because I need to sanitize any possible StackTrace that appears (otherwise it's very hard to read since it's got all the Groovy meta-code). Is there any way to specify that all JUnit tests get wrapped in particular way (like an error handler)?
Note: I am running these in Eclipse, but if there's a way to do this in IntelliJ or Netbeans, that would be good to know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用规则。基本上,您必须有一个实现 MethodRule 接口的类,该类通过替换其自己的包含 try/catch 的 Statement 实现来处理 apply 方法中的异常处理。
要使用规则,您可以在测试类中定义一个字段,如下所示:
第一次实现可能看起来像这样:
上面的内容完全未经测试,但您应该能够理解这个想法。 @Rule 注释是在 JUnit 4.7 中引入的,因此您可能需要更新才能使用它。
Yes, use a Rule. Basically you have to have a class which implements the MethodRule interface that handles the exception handling in the apply method by substituting its own Statement implementation that has the try/catch in it.
To use a rule you define a field in the test class like so:
A first cut implementation would probably look something like this:
The above is totally untested, but you should be able to get the idea. The @Rule annotation was introduced in JUnit 4.7, so you may need to update to use it.