使用JUnitCore运行时,TestWatcher不会捕获失败的测试

时间:2018-05-08 16:18:51

标签: junit

如果我右键单击并从我的IDE运行此休息,测试类SimpleJunit将失败并显示此输出

2018-05-08 09:13:38.297-0700 D SimpleJunit starting()
2018-05-08 09:13:38.301-0700 D SimpleJunit before()
2018-05-08 09:13:38.302-0700 D SimpleJunit after()

junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:55)
    at junit.framework.Assert.fail(Assert.java:64)
    at junit.framework.TestCase.fail(TestCase.java:235)
    at srcNew.SimpleJunit.before(SimpleJunit.java:18)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:55)
    at junit.framework.Assert.fail(Assert.java:64)
    at junit.framework.TestCase.fail(TestCase.java:235)
    at srcNew.SimpleJunit.after(SimpleJunit.java:32)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


2018-05-08 09:13:38.314-0700 D SimpleJunit succeeded()
2018-05-08 09:13:38.315-0700 D SimpleJunit finished()

以下是SimpleJunit

public class SimpleJunit {

    private static String TAG = SimpleJunit.class.getSimpleName();

    @Before
    public void before(){
        Log.d(TAG,"before()");
        fail();
        throw new RuntimeException();
    }

    @Test
    public void test(){
        Log.d(TAG,"test()");
        fail();
        throw new RuntimeException();
    }

    @After
    public void after(){
        Log.d(TAG,"after()");
        fail();
        throw new RuntimeException();
    }

    @ClassRule
    public static TestRule classWatchman = new TestWatcher()
    {
        @Override
        protected void starting(Description d)
        {
            Log.d(TAG,"starting()");
        }

        @Override
        protected void failed(Throwable e, Description d)
        {
            Log.d(TAG,"failed()");
        }

        @Override
        protected void succeeded(Description d)
        {
            Log.d(TAG,"succeeded()");
        }

        @Override
        protected void finished(Description description)
        {
            Log.d(TAG,"finished()");
        }

        @Override
        protected void skipped(AssumptionViolatedException e,
                               Description description)
        {
            Log.d(TAG,"skipped()");
        }
    };

}

如果我从另一个类SimpleJunit运行SimpleJunitRunner,测试将通过此输出,即使测试应该明显失败。

public class SimpleJunitRunner {

    private static String TAG = SimpleJunitRunner.class.getSimpleName();

    public static void main(String[] ars){
        Result result = JUnitCore.runClasses(SimpleJunit.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
    }
}

输出

2018-05-08 09:17:34.089-0700 D SimpleJunit starting()
2018-05-08 09:17:34.092-0700 D SimpleJunit before()
2018-05-08 09:17:34.094-0700 D SimpleJunit after()
test(srcNew.SimpleJunit): null
test(srcNew.SimpleJunit): null
2018-05-08 09:17:34.099-0700 D SimpleJunit succeeded()
2018-05-08 09:17:34.100-0700 D SimpleJunit finished()

Process finished with exit code 0

我不确定我是否使用了API错误,或者我在这里发生了什么。

1 个答案:

答案 0 :(得分:0)

JUnitCore似乎无法与TestWatcher一起正常工作。即使在发出断言失败或直接抛出异常时,TestWatcher.succeeded()方法也会触发。

然而,我确实找到了等待使用JUnitCore打印出故障的堆栈跟踪,所以至少还有一种可靠的方法来捕获失败的测试。

public class SimpleJunitRunner {

    private static String TAG = SimpleJunitRunner.class.getSimpleName();

    public static void main(String[] ars){
        Result result = new JUnitCore().run(SimpleJunit.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.getTrace());
        }
    }
}