Han*_*örr 7 java junit intellij-idea
当我进行JUnit测试时,我希望调试器能够在测试代码中出现任何异常的位置停止,以便测试失败,以便检查错误.是否有可能配置IntelliJ的调试器来做到这一点,而不是打破其他异常?
如果我在任何未捕获的异常上设置了异常断点,那么这不起作用,因为JUnit捕获了异常.如果我尝试使用catch类org.junit对捕获的异常进行断点中断,那也不起作用,因为异常在Javas反射机制到达JUnit之前被Javas反射机制捕获并包装.所以我在这里有点不知所措 - Eclipse只是停在原来的例外.
澄清:我在讨论代码I测试中的异常或从那里调用的代码,而不是测试中的断言失败.例如,考虑以下测试:
@Test
public void thisShouldBreak() {
int i = 25 - 25;
int j = 17 / i;
}
private void neverBreakHereSinceThisIsCaught() {
int i = 14 - 14;
int j = 29 / i;
}
@Test
public void thisShouldNotBreak() {
try {
neverBreakHereSinceThisIsCaught();
} catch (ArithmeticException e) {
// ignored or handled
}
}
@Test
public void thisShouldNotBreakEither() {
try {
getClass().getDeclaredMethod("neverBreakHereSinceThisIsCaught").invoke(this);
} catch (Exception e) {
// ignored or handled
}
}
Run Code Online (Sandbox Code Playgroud)
我希望IntelliJ thisShouldBreak在抛出ArithmeticException的地方执行测试时停止,这样我就可以检查导致异常的i的值.但是,我不希望IntelliJ停止,neverBreakHereSinceThisIsCaught因为抛出的异常没有到达JUnit.我尝试失败了: - neverBreakHereSinceThisIsCaught, too, and loads of other places.
- an exception breakpoint only on uncaught exception is never hit at all, since JUnit catches and wraps those exceptions.
- a catch class filterorg.junit中被捕获的异常中断的异常断点.*`JUnit的JUnit结束Java反射调用的许多内部位置中断了.
这就是我所做的:
您基本上想要做的是告诉 Intellij 在 catch 类过滤器是 junit 的情况下停止任何异常(捕获和未捕获),以便仅由 junit 捕获的异常导致调试器停止。
当您使用像 Tomcat 这样的应用程序服务器时,您可能会遇到类似的问题,它会捕获异常。同样,您可以通过 Tomcat 包过滤 catch 类。