JUnit 4和暂停异常 [英] JUnit 4 and Suspend-on-Exception

查看:183
本文介绍了JUnit 4和暂停异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的代码中抛出一个未捕获的异常时,我习惯让调试器停在抛出语句中,以便我可以检查局部变量和抛出异常时所涉及到的所有对象的成员。使用 IntelliJ Idea ,可以通过转到运行查看断点,选择异常断点选项卡,检查任何异常,并确保捕获异常复选框未选中,而检查未捕获异常复选框。使用 Eclipse Visual Studio (对于C#),它是不同的,但沿着相同的行。

When an uncaught exception is thrown in my code, I am used to having the debugger stop at the throwing statement so that I can examine the local variables and the members of all objects involved at the moment that the exception was thrown. With IntelliJ Idea this can be accomplished by going to Run, View Breakpoints, selecting the Exception Breakpoints tab, checking Any exception, and making sure that the Caught exception checkbox is unchecked, while the Uncaught exception checkbox is checked. With Eclipse and with Visual Studio (for C#) it is something different, but along the same lines.

让调试器以这种方式行事的能力非常有用;实际上这样有用,我相应地构建了我的程序的主循环:在发布版本上,主循环当然嵌入在一个try-catch-all中;但是在调试版本中,主循环中没有try-catch块,所以在程序中任何地方没有捕获到的任何异常都将被忽略,所以调试器会在抛出它的时候暂停我的程序。

The ability to have the debugger behave this way is extremely useful; so useful in fact, that I structure the main loop of my programs accordingly: on the release version, the main loop is of course embedded inside a try-catch-all; but on the debug version, there is no try-catch block in the main loop, so that any exceptions that are not caught anywhere within my program will remain uncaught, so that the debugger will suspend my program at the moment that they are thrown.

然而,当使用JUnit测试我的Java类时,我有一个问题:调试器不会停止任何异常。相反,发生的是不仅预期,而且意外的异常被自动捕获,并且我被给予一个验证后的异常堆栈跟踪来尝试和理解。这不是很酷。

When testing my Java classes with JUnit, however, I have a problem: the debugger does not stop on any exception. Instead, what happens is that not only expected, but also unexpected exceptions get automagically caught, and I am given a post-mortem exception stack trace to try and make sense out of. That's not very cool.

我以前认为这是发生的,因为JUnit使用 java.lang.reflect.Method.invoke() ,它捕获所有异常并将其转换为 TargetInvocationExceptions ,但后来我写了自己的自定义JUnit运行器,它知道我的测试类亲自直接调用其方法没有 Method.invoke(),问题仍然存在。这意味着这个问题在核心JUnit中是高涨的。

I used to think that this is happening because JUnit makes use of java.lang.reflect.Method.invoke(), which catches all exceptions and transforms them to TargetInvocationExceptions, but then I wrote my own custom JUnit runner, which knows my test class personally and directly invokes its methods without Method.invoke(), and the problem persists. This means that the problem lies high up within core JUnit.

那么其他任何人都有同样的问题吗?有没有人知道一个解决方案,以便在使用JUnit测试时,我们可以让调试器暂停程序执行意外异常?

So, does anyone else have the same problem? Does anyone know of a solution so that we can have the debugger suspend program execution on unexpected exceptions while testing with JUnit?

相关(未回答)问题:在Eclipse Junit测试运行器中悬挂未捕获的运行时异常

Related (unanswered) question: Suspend on uncaught runtime exceptions in Eclipse Junit test runner

相关(未回覆)问题:如何在jUnit测试用例中插入调试器?

Related (unanswered) question: How to break into debugger within a jUnit test case?

相关(部分回答)问题:使用jUnit在Eclipse中使用异常(接受的答案说如果您调试单个方法jUnit,断点开始工作,如果整个类或包在jUnit中被调试,调试器不起作用。)

Related (partly answered) question: Break on Exception in Eclipse using jUnit (The accepted answer says that "if you debug a single method in jUnit, the breakpoints start to work. If an entire class or package is debugged in jUnit, the debugger doesn't work.")

解决方案

我希望我正确理解问题,如果我错了,请纠正我的意见,但是:

I hope I understand the question correctly, so correct me if I'm wrong, but:


    你有一个测试,它引发了一个异常,你不会抓住自己(所以在这个意义上,没有被捕获)
  • 当你使用JUnit4 testrunner调试这个测试,JUnit显示测试失败(它抛出异常)

  • 但是线程不被挂起,这是你想要实现的

  • 即使您有首选项| java |调试|暂停执行未捕获的异常启用

  • you have a test, which throws an exception, which you do not catch yourself (so it, in that sense, is uncaught)
  • when you debug this test with the JUnit4 testrunner, JUnit shows you that the test fails (it threw the exception)
  • but the thread is not suspended, which is what you want to achieve
  • even though you have preferences | java | debug | suspend execution on uncaught exceptions enabled

在这种情况下,我相信这是预期的行为,JUnit捕获并吞下了你的异常,所以就eclipse而言,这是不会被忽略的,如这里所述 https://bugs.eclipse.org/bugs/show_bug.cgi?id=414133

In that case, I believe this is the intended behavior, JUnit catches and swallows your exception, so it is not uncaught as far as eclipse is concerned, as described here https://bugs.eclipse.org/bugs/show_bug.cgi?id=414133

您可以通过设置您的自己的规则暂停捕获异常。 Debug perspective |断点视图| J!图标|检查挂起异常情况

You can have eclipse suspend any way by setting up your own rules for suspending on caught exceptions. Debug perspective | breakpoint view | J! icon | check suspend on caught exception

这篇关于JUnit 4和暂停异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆