如何只打印自定义异常的痕迹?而是测试NG跟踪


class use extends Test {
@org.testng.annotations.Test
public void test() throws exc {
try {
System.out.println("test");
int a = 1 / 0;
System.out.println(a);
} catch (ArithmeticException e) {
throw new exc("This is EXC error");
}
}       
class exc extends Exception {
private static final long serialVersionUID = 1L;
public exc(String a) {
super(a);
}
}

  1. 我只想在控制台中打印前两行
  2. 如果我从Main方法执行,可以做到这一点,但使用TestNG会抛出额外的跟踪,如图所示

这是控制台

FAILED: test
runner.exc: This is EXC error
at runner.use.test(Test.java:47)

我想打印到这里,但控制台也显示了以下内容

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

您可以使用此代码以字符串形式获取第一行thwo:

// tr is the exception object
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw); // Replace tr with your exception object
String[] stl = sw.toString().split("n");
// Get the first two lines
// Use deleteCharAt(sb.length() - 1) to get rid of r at the end of the line (You may not need to this)
StringBuilder sb = new StringBuilder();
if (stl.length > 0) sb.append(stl[0]).deleteCharAt(sb.length() - 1).append('n');
if (stl.length > 1) sb.append(stl[1]).deleteCharAt(sb.length() - 1).append('n');
// Use sb.toString() to get the result

相关内容

  • 没有找到相关文章