这是我的测试代码:
@RunWith(AndroidJUnit4.class)
@SmallTest
public class WelcomeActivityTests extends BaseTest {
ApplicationController applicationController;
@Rule
public ActivityTestRule<WelcomeActivity> activityTestRule = new ActivityTestRule<>(WelcomeActivity.class);
ArgumentCaptor<Callback> argumentCaptor;
@Before
@Override public void setUp() {
applicationController = (ApplicationController) InstrumentationRegistry.getTargetContext().getApplicationContext();
applicationController.setMockMode(true);
argumentCaptor = ArgumentCaptor.forClass(Callback.class);
super.setUp();
}
@Test
public void testLogin() throws InterruptedException {
onView(withId(R.id.btnLogInW)).perform(click());
onView(withId(R.id.email)).perform(typeText("good.email@example.com"));
onView(withId(R.id.passL)).perform(typeText("strong.password"));
onView(withId(R.id.btnLogInL)).perform(click());
User user = new User();
user.first_name = "Fake name";
user.last_name = "Fake name";
user.id = 1;
user.email = "fake.email@gmail.com";
AuthResponse authResponse = new AuthResponse();
authResponse.api_key = "fake_api_key";
authResponse.status = "ok";
authResponse.user = user;
Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
argumentCaptor.getValue().success(authResponse, null);
assert true;
}
@After
public void release() {
applicationController.setMockMode(false);
}
}
按钮我点击使用espresso是基于材料主题,他们可能有一些动画下面。结果是,当我尝试运行一些工具测试时,每次我执行点击时应用程序都会崩溃。我试过在系统开发人员选项中关闭动画,但没有帮助。
android.util。AndroidRuntimeException: Animators可能只能运行在android.animation.ValueAnimator.start (ValueAnimator.java: 1002)android.animation.ValueAnimator.start (ValueAnimator.java: 1050)android.animation.ObjectAnimator.start (ObjectAnimator.java: 829)android.animation.AnimatorSet.start (AnimatorSet.java: 585)android.animation.StateListAnimator.start (StateListAnimator.java: 187)在android.animation.StateListAnimator.setState (StateListAnimator.java: 180)在android.view.View.drawableStateChanged(View.java:15988) atandroid.widget.TextView.drawableStateChanged (TextView.java: 3659)android.view.View.refreshDrawableState (View.java: 16032)android.view.View.setEnabled (View.java: 6724)android.widget.TextView.setEnabled (TextView.java: 1446)my.app.ui.fragments.welcome.LoginFragment.unlock (LoginFragment.java: 263)在my.app.ui.fragments.welcome.LoginFragment 4.美元成功(LoginFragment.java: 224)在my.app.ui.fragments.welcome.LoginFragment 4.美元成功(LoginFragment.java: 222)在my.app.WelcomeActivityTests.testRate (WelcomeActivityTests.java: 84)在java.lang.reflect.Method。调用(本机方法)java.lang.reflect.Method.invoke (Method.java: 372)org.junit.runners.model.FrameworkMethod runreflectivecall 1.美元(FrameworkMethod.java: 45)在org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java: 15)在org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java: 42)在org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java: 20)在org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java: 28)在org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java: 30)在android.support.test.internal.statement.UiThreadStatement.evaluate (UiThreadStatement.java: 55)在android.support.test.rule.ActivityTestRule ActivityStatement.evaluate美元(ActivityTestRule.java: 257)(RunRules.java:18org.junit.runners.ParentRunner.runLeaf (ParentRunner.java: 263)org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 68)在org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 47)在org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) atorg.junit.runners.ParentRunner时间表(ParentRunner.java: 60) 1.美元org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 229)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 50)org.junit.runners.ParentRunner评估(ParentRunner.java: 222) 2.美元org.junit.runners.ParentRunner.run (ParentRunner.java: 300)org.junit.runners.Suite.runChild (Suite.java: 128)org.junit.runners.Suite.runChild (Suite.java: 24)org.junit.runners.ParentRunner 3.美元(ParentRunner.java: 231)运行org.junit.runners.ParentRunner时间表(ParentRunner.java: 60) 1.美元org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 229)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 50)org.junit.runners.ParentRunner评估(ParentRunner.java: 222) 2.美元org.junit.runners.ParentRunner.run (ParentRunner.java: 300)org.junit.runner.JUnitCore.run (JUnitCore.java: 157)org.junit.runner.JUnitCore.run (JUnitCore.java: 136)android.support.test.internal.runner.TestExecutor.execute (TestExecutor.java: 54)在android.support.test.runner.AndroidJUnitRunner.onStart (AndroidJUnitRunner.java: 228)在android.app.Instrumentation InstrumentationThread.run美元(Instrumentation.java: 1853)
这一行会导致我的应用程序崩溃:
btnFacebook.setEnabled(false);
编辑:我找到了合适的解决方案,寻找公认的答案。
好的,我找到了一个合适的解决方案!当处理使用处理程序的库和api时,您需要用@UiThreadTest注释您的测试用例。此外,您正在存根的每个异步回调都应该使用Instrumentation的runOnMainSync(Runnable r)方法调用。例子:
@Test
@UiThreadTest
public void testLoginSuccess() {
Instrumentation.ActivityMonitor monitor = InstrumentationRegistry.getInstrumentation().addMonitor(EventsListActivity.class.getName(), null, true);
onView(withId(R.id.btnLogInW)).perform(click());
onView(withId(R.id.email)).perform(typeText("good.email@example.com"));
onView(withId(R.id.passL)).perform(typeText("strong.password"));
onView(withId(R.id.btnLogInL)).perform(click());
User user = new User();
user.first_name = "Fake name";
user.last_name = "Fake name";
user.id = 1;
user.email = "fake.email@gmail.com";
final AuthResponse authResponse = new AuthResponse();
authResponse.api_key = "fake_api_key";
authResponse.status = "ok";
authResponse.user = user;
Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override public void run() {
argumentCaptor.getValue().success(authResponse, null);
}
});
assertThat(1, equalTo(monitor.getHits()));
InstrumentationRegistry.getInstrumentation().removeMonitor(monitor);
}
尝试在模拟线程中调用该行:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btnFacebook.setEnabled(false);
}
}, 100);