我想向控制台报告我的异常堆栈跟踪,所以如果我有:
@BeforeStories
public void init() {
throw new RuntimeException("omg");
}
我想:
java.lang.RuntimeException: omg
at com.mycompany.MyClass.init(MyClass.java:69)
这可能吗?
答案 0 :(得分:0)
我不确定我是否理解您尝试实现的目标,因为抛出异常应该已经将失败记录到控制台。
但是如果你想打印出异常消息并将tracestack打印到控制台,你可以按照以下方式进行操作,虽然它非常糟糕,它可以给你你想要的东西:
@BeforeStories
public void runBeforeAllStories() {
new RuntimeException("omg").printStackTrace();
}
应用程序将继续运行,但会在控制台中输出以下文本:
java.lang.RuntimeException:omg at jbehave.sample.steps.LifecycleSteps.runBeforeAllStories(LifecycleSteps.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)at org.jbehave.core.steps.StepCreator $ MethodInvoker.invoke(StepCreator.java:805) 在 org.jbehave.core.steps.StepCreator $ BeforeOrAfterStep.perform(StepCreator.java:491) 在 org.jbehave.core.embedder.PerformableTree $ FineSoFar.run(PerformableTree.java:340) 在 org.jbehave.core.embedder.PerformableTree $ PerformableSteps.perform(PerformableTree.java:1072) 在 org.jbehave.core.embedder.PerformableTree.performBeforeOrAfterStories(PerformableTree.java:427) 在 org.jbehave.core.embedder.StoryManager.performStories(StoryManager.java:117) 在 org.jbehave.core.embedder.StoryManager.runStories(StoryManager.java:107) 在 org.jbehave.core.embedder.StoryManager.runStoriesAsPaths(StoryManager.java:86) 在 org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:213) 在jbehave.sample.common.StoriesTest.run(StoriesTest.java:161)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at at org.junit.runner.JUnitCore.run(JUnitCore.java:160)at at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) 在 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
答案 1 :(得分:0)
首先,我很欣赏您希望以有趣的方式展示您的例外情况。你可以表达你的异常,就像在@BeforeStories方法中添加throws Exception一样,如下所示。
@BeforeStories
public void init() throws Exception{
throw new RuntimeException("omg");
}