The @UiThreadTest annotation creates "java.lang.Exception: no runnable methods"

I am 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 the view hierarchy can touch its views.

I read that using the @UiThreadTest annotation will cause my test case to run in the ui thread and resolve the error. However, when I use it, I get this error: java.lang.Exception: no managed methods

I added UiThreadTestRule and ensured that I import android.support.test.annotation.UiThreadTest, but to no avail.

Is there anything else I am 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); } } 
+5
source share

Source: https://habr.com/ru/post/1244449/


All Articles