简体   繁体   English

通过创建未抛出的“新Exception()”来在测试时隐藏JUnit的堆栈跟踪?

[英]Hide JUnit's stack trace when testing by creating a “new Exception()” that isn't thrown?

I am testing one of my methods w/ JUnit and the method takes an Exception as a parameter. 我正在测试带有JUnit的方法之一,并且该方法将Exception作为参数。 The test looks roughly like this: 测试大致如下所示:

public void testSomething() {
...
  callMethod(param1, new Exception("ignore exception"));
}

When I do this, the act of creating an Exception (not throwing it) dumps this to System.err 当我这样做时,创建异常(不抛出异常)的操作会将其转储到System.err

java.lang.Exception: ignore exception at ...testSomething at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:176) at junit.framework.TestCase.runBare(TestCase.java:141 java.lang.Exception:在.... testSomething处忽略异常java.lang.reflect.Method.invoke(未知源)位于junit.framework.TestCase.runTest(TestCase.java:176)at junit.framework.TestCase.runBare(TestCase.java:141)

Is there any way to suppress this output? 有什么办法可以抑制此输出? I Goggled for everything I could think of, but it picks up all sorts of unrelated stuff. 我全神贯注地想尽一切办法,但它收集了各种无关的东西。

仅供参考,函数调用在其中做了一个ex.printStackTrace()调试语句,我忘了取出它,所以亚当是正确的。

您可以在不调用父构造函数的情况下从Exception派生类吗?

暂无
暂无

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

相关问题 为什么我的Java异常在SwingWorker中抛出时不会打印堆栈跟踪? - Why doesn't my Java exception print a stack trace when thrown inside a SwingWorker? 如何停止EJBException的堆栈跟踪(在每个抛出的Exception中) - How to stop EJBException's stack trace (in every thrown Exception) Junit Eclipse - 预期异常时没有成功的堆栈跟踪 - Junit Eclipse - No stack trace on success when exception is expected Java 错误:catch 块中抛出新异常,原始堆栈跟踪可能丢失 - Java error: New exception is thrown in catch block, original stack trace may be lost 在Eclipse中引发未经检查的异常时,从新创建的线程打印堆栈跟踪 - Printing the stack trace from a newly created thread when an unchecked exception is thrown in Eclipse ScheduledExecutorService的任务引发异常时,不打印堆栈跟踪 - No stack trace is printed when ScheduledExecutorService's task throws an exception 隐藏/处理servlet异常堆栈跟踪输出 - Hide/handle servlet exception stack trace output Junit(3.8.1)测试是否引发了异常(在单元测试中有效,添加到testSuite时失败) - Junit (3.8.1) testing that an exception is thrown (works in unit test, fails when added to a testSuite) 多次抛出相同异常后省略堆栈跟踪 - Omit stack trace after same exception is thrown several times 如何查找Swing组件抛出的未知异常的(信息性)堆栈跟踪 - How to find an (informative) stack trace of unknown exception thrown by Swing components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM