简体   繁体   English

Junit Eclipse - 预期异常时没有成功的堆栈跟踪

[英]Junit Eclipse - No stack trace on success when exception is expected

I have junit tests that are using @Test(expected=) syntax.我有使用 @Test(expected=) 语法的 junit 测试。 When I run those tests the exceptions are generated and the tests pass, but the exceptions' stack trace is still logged in the console.当我运行这些测试时,会生成异常并且测试通过,但异常的堆栈跟踪仍记录在控制台中。

This is confusing because there is no indication of which test generated what message.这是令人困惑的,因为没有迹象表明哪个测试生成了什么消息。 I have a lot of test and I don't know if those stack traces are just the expected exceptions or there is some problem in the test.我有很多测试,我不知道这些堆栈跟踪是否只是预期的异常,或者测试中存在一些问题。

Is there a way to prevent tests that expect exceptions to be thrown from logging if the test succeeds?如果测试成功,有没有办法阻止期望从日志中抛出异常的测试?

Yes.是的。 Don't log them.不要记录它们。 It's not JUnit printing that stuff.这不是 JUnit 打印那些东西。 It's you, in your own (non-test) code.是你,在你自己的(非测试)代码中。 JUnit can't magically dive in there, figure out that you wrote code that explicitly tells the VM to print the trace for no good reason, and somehow suppress that. JUnit 不能神奇地潜入那里,找出您编写的代码明确告诉 VM 无缘无故地打印跟踪,并以某种方式抑制它。

It's not your fault, it's.. a ton of tutorials and even here on SO, answers, as well as crazy IDE defaults that led you down this path.这不是你的错,而是......大量的教程,甚至在这里,答案,以及导致你走上这条道路的疯狂 IDE 默认设置。

So, fix it!所以,修复它! It's not too difficult;这不是太难; fire up your IDE's global search tool and go hunt for e.printStackTrace() .启动 IDE 的全局搜索工具并寻找e.printStackTrace() Eliminate them all .将它们全部消除 It's hunting season.现在是狩猎季节。

So, how would you do this?那么,你会怎么做呢? A few steps:几个步骤:

  1. First things first: Enter your IDE settings and fix the template when using the quick-fix of 'add try/catch'.首先要做的事情是:输入您的 IDE 设置并在使用“添加 try/catch”的快速修复时修复模板。 The catch block should be throw new RuntimeException("uncaught", e); catch 块应该是throw new RuntimeException("uncaught", e); , not e.printStackTrace() . ,而不是e.printStackTrace()

  2. Any time a method's nature inherently implies that it throws a certain checked exception, make it do so.任何时候一个方法的本质固有地暗示它会抛出一个特定的检查异常,让它这样做。 A method named openFile that does not throws IOException is just badly written.一个throws IOException名为openFile的方法写得不好。 Make it throws that, then eliminate the try/catch(Exception e) {e.printStackTrace();} which we shall henceforth name 'the dodo pattern'.让它抛出那个,然后消除try/catch(Exception e) {e.printStackTrace();} ,我们以后将其命名为“渡渡鸟模式”。 That should get rid of about half of them.这应该摆脱大约一半的人。

  3. Next up are places where the checked exceptions you 'fixed' with the dodo pattern are not inherently part of a method's nature.接下来是您使用 dodo 模式“修复”的已检查异常并不是方法本质的固有部分。 For example, let's say you wrote a game save system and your current implementation works by writing it out to a DB, and you dodo-patterned the SQLException .例如,假设您编写了一个游戏保存系统,并且您当前的实现通过将其写入数据库来工作,并且您对SQLException进行了 dodo 模式。 Save games do not inherently imply DB interactions, so writing public void saveGame() throws SQLException isn't necessarily good design.保存游戏本身并不意味着数据库交互,因此编写public void saveGame() throws SQLException不一定是好的设计。 The solution in pretty much all such caes is to rewrap the exception into something else.几乎所有此类情况的解决方案是将异常重新包装为其他内容。 For example, make a SaveException class.例如,创建一个SaveException类。 Ensure it has the constructor that takes a message and a cause.确保它具有接受消息原因的构造函数。 Then:然后:

} catch (SQLException e) {
  throw new SaveException("Cannot save game", e);
}

Keep the message short, sweet, don't add superfluous info (If your message involves "Something went wrong" - you're doing it wrong. It's an exception that ended up uncaught. That something is wrong is implied, no need to say it).保持消息简短,甜蜜,不要添加多余的信息(如果您的消息涉及“出现问题”-您做错了。这是一个最终未被发现的例外。暗示出了问题,无需说它)。 and definitely don't add exclamation marks.并且绝对不要添加感叹号。

Sometimes you don't need to make an exception and can rewrap into something more useful.有时你不需要例外,可以重新包装成更有用的东西。

  1. That leaves checked exceptions that you nevertheless doubt can actually ever really occur.这留下了您仍然怀疑是否真的会发生的已检查异常。 Rewrap those too, into RuntimeException or some other simplistic thing, or use lombok's @SneakyThrows for these.也将它们重新包装到RuntimeException或其他一些简单的东西中,或者使用@SneakyThrows@SneakyThrows来实现这些。

That should let you get rid of each and every one of those pesky .printStackTrace() calls.这应该让您摆脱每一个讨厌的.printStackTrace()调用。 Once you've done that, your problem will go away.一旦你这样做了,你的问题就会消失。

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

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