JUnit 4和Suspend-on-Exception [英] JUnit 4 and Suspend-on-Exception

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

问题描述

当我的代码中抛出一个未捕获的异常时,我习惯于将调试器停在抛出语句中,以便在抛出异常的时刻检查局部变量和所有对象的成员。使用 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

您可以通过设置您的eclipse挂起任何方式自己的规则暂停捕获异常。 Debug perspective |断点视图| J 1图标|检查挂起异常

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和Suspend-on-Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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