Annotation @UiThreadTest produces "java.lang.Exception: No runnable methods"

时间:2016-03-04 17:59:36

标签: android unit-testing junit4

I have been trying to write a unit test that works with system bars. I was getting errors about trying to access things from the wrong thread: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I have read that using the @UiThreadTest annotation will cause my test case to run on the ui thread and resolve the error. However, when I use it I receive this error: java.lang.Exception: No runnable methods

I have added the UiThreadTestRule and ensured thatI'm importing android.support.test.annotation.UiThreadTest, but to no avail.

Is there something else I'm missing?

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import android.support.test.annotation.UiThreadTest;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)


/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
public class SystemBarSetterTest extends ActivityInstrumentationTestCase2<SystemBarSetter>
{

    private SystemBarSetter setter;
    public SystemBarSetterTest() {
        super(SystemBarSetter.class);
    }

    @Rule
    public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule();

    @Before
    public void setUp() throws Exception
    {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        setter = getActivity();
    }

    @Override
    public void runTestOnUiThread(Runnable r) throws Throwable {
        super.runTestOnUiThread(r);
    }

    @Test
    public void testImmersiveSticky() throws Exception
    {
        setter.setImmersiveStickyMode();
        int requiredFlags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        assertEquals(requiredFlags, setter.getCurrentUiVisibility()&requiredFlags);
    }
}

0 个答案:

没有答案