Junit assumeTrue conditionally skip test

org.junit.Assume.assumeTrue(false) in @BeforeClass does not work as expected.

Code:

package com.XXX.XXX;

import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class DummyTest {

    private static final String TAG = DummyTest.class.getSimpleName() + "TAG";

    @BeforeClass
    public static void setUpBeforeClass() {
        Log.v(TAG, "@BeforeClass part 01.");
        org.junit.Assume.assumeTrue(false);
        Log.v(TAG, "@BeforeClass part 02.");
    }

    @AfterClass
    public static void tearDownAfterClass() {
        Log.v(TAG, "@tearDownAfterClass.");
    }

    @Before
    public void setUp() throws Exception {
        Log.v(TAG, "@setUp.");
    }

    @After
    public void tearDown() throws Exception {
        Log.v(TAG, "@tearDown.");
    }

    @Test
    public void test01() throws Exception {
        Log.v(TAG, "@Test test01.");
    }

    @Test
    public void test02() throws Exception {
        Log.v(TAG, "@Test test02.");
    }
}

Output:

V/DummyTestTAG: @BeforeClass part 01.
V/DummyTestTAG: @tearDownAfterClass.

Execute with command Run

Empty test suite.
No tests were found

Execute with command Debug

I/AndroidJUnitRunner: Debugger connected.
I/MonitoringInstrumentation: Instrumentation Started!
I/MonitoringInstrumentation: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.marvell.debu.connection.test-izri5KTxT3YmFP0OAM6KWQ==/base.apk", zip file "/data/app/com.marvell.debu.connection-mFkqsxmQR58pnLfmX3rVqQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.marvell.debu.connection.test-izri5KTxT3YmFP0OAM6KWQ==/lib/arm, /data/app/com.marvell.debu.connection-mFkqsxmQR58pnLfmX3rVqQ==/lib/arm, /system/lib, /system/vendor/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.marvell.debu.connection.test-izri5KTxT3YmFP0OAM6KWQ==/base.apk", zip file "/data/app/com.marvell.debu.connection-mFkqsxmQR58pnLfmX3rVqQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.marvell.debu.connection.test-izri5KTxT3YmFP0OAM6KWQ==/lib/arm, /data/app/com.marvell.debu.connection-mFkqsxmQR58pnLfmX3rVqQ==/lib/arm, /system/lib, /system/vendor/lib]]]'
I/MonitoringInstrumentation: No JSBridge.
I/UsageTrackerFacilitator: Usage tracking enabled
D/TestExecutor: Adding listener android.support.test.internal.runner.listener.LogRunListener
D/TestExecutor: Adding listener android.support.test.internal.runner.listener.InstrumentationResultPrinter
D/TestExecutor: Adding listener android.support.test.internal.runner.listener.ActivityFinisherRunListener
I/TestRunner: run started: 2 tests
V/DummyTestTAG: @BeforeClass part 01.
V/DummyTestTAG: @tearDownAfterClass.
I/TestRunner: assumption failed: com.marvell.debu.connection.DummyTest
I/TestRunner: ----- begin exception -----
I/TestRunner: org.junit.AssumptionViolatedException: got: , expected: is 
                  at org.junit.Assume.assumeThat(Assume.java:95)
                  at org.junit.Assume.assumeTrue(Assume.java:41)
                  at com.XXX.XXX.DummyTest.setUpBeforeClass(DummyTest.java:21)
                  at java.lang.reflect.Method.invoke(Native Method)
                  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.run(ParentRunner.java:363)
                  at org.junit.runners.Suite.runChild(Suite.java:128)
                  at org.junit.runners.Suite.runChild(Suite.java:27)
                  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.runners.ParentRunner.run(ParentRunner.java:363)
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                  at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
                  at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
                  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
I/TestRunner: ----- end exception -----
I/TestRunner: failed: Test mechanism
I/TestRunner: ----- begin exception -----
I/TestRunner: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Bundle.putString(java.lang.String, java.lang.String)' on a null object reference
                  at android.support.test.internal.runner.listener.InstrumentationResultPrinter.testAssumptionFailure(InstrumentationResultPrinter.java:173)
                  at org.junit.runner.notification.SynchronizedRunListener.testAssumptionFailure(SynchronizedRunListener.java:70)
                  at org.junit.runner.notification.RunNotifier$5.notifyListener(RunNotifier.java:160)
                  at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72)
                  at org.junit.runner.notification.RunNotifier.fireTestAssumptionFailed(RunNotifier.java:157)
                  at org.junit.internal.runners.model.EachTestNotifier.addFailedAssumption(EachTestNotifier.java:34)
                  at org.junit.runners.ParentRunner.run(ParentRunner.java:365)
                  at org.junit.runners.Suite.runChild(Suite.java:128)
                  at org.junit.runners.Suite.runChild(Suite.java:27)
                  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.runners.ParentRunner.run(ParentRunner.java:363)
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                  at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
                  at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
                  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
I/TestRunner: ----- end exception -----
I/TestRunner: run finished: 0 tests, 1 failed, 0 ignored
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/MonitoringInstrumentation: waitForActivitiesToComplete() took: 0ms
I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0

Started running tests
Tests ran to completion.


Disconnected from the target VM, address: 'localhost:8695', transport: 'socket'
Empty test suite.

Somehow there is a NPE.

@Override
    public void testAssumptionFailure(Failure failure) {
        mTestResultCode = REPORT_VALUE_RESULT_ASSUMPTION_FAILURE;
        mTestResult.putString(REPORT_KEY_STACK, failure.getTrace());
    }

It seems this is where goes wrong:

//Package: android.support.test.internal.runner.listener
//Class: InstrumentationResultPrinter
Bundle mTestResult;
......
mTestResult.putString(REPORT_KEY_STACK, failure.getTrace());
So I have to skip test one by one:
   @Test
    public void test01() throws Exception {
        org.junit.Assume.assumeTrue(false);
    }

    @Test
    public void test02() throws Exception {
        org.junit.Assume.assumeTrue(false);
    }

你可能感兴趣的:(Junit assumeTrue conditionally skip test)