这是我的测试代码:
@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);
}
}
按钮我点击使用浓缩咖啡是基于材料主题,他们可能在下面有一些动画。结果是,当我尝试运行一些入侵测试时,应用程序每次执行单击时都会崩溃。我尝试在系统开发人员选项中关闭动画,但它没有帮助。
android.util.AndroidRuntimeException:动画师只能运行 Looper线程在 android.animation.ValueAnimator.start(ValueAnimator.java:1002)at android.animation.ValueAnimator.start(ValueAnimator.java:1050)at android.animation.ObjectAnimator.start(ObjectAnimator.java:829)at android.animation.AnimatorSet.start(AnimatorSet.java:585)at android.animation.StateListAnimator.start(StateListAnimator.java:187) 在 android.animation.StateListAnimator.setState(StateListAnimator.java:180) 在android.view.View.drawableStateChanged(View.java:15988)处 android.widget.TextView.drawableStateChanged(TextView.java:3659)at android.view.View.refreshDrawableState(View.java:16032)at android.view.View.setEnabled(View.java:6724)at android.widget.TextView.setEnabled(TextView.java:1446)at my.app.ui.fragments.welcome.LoginFragment.unlock(LoginFragment.java:263) 在 my.app.ui.fragments.welcome.LoginFragment $ 4.success(LoginFragment.java:224) 在 my.app.ui.fragments.welcome.LoginFragment $ 4.success(LoginFragment.java:222) 在 my.app.WelcomeActivityTests.testRate(WelcomeActivityTests.java:84) 在java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(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) 在org.junit.rules.RunRules.evaluate(RunRules.java:18)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at at 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)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)at at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at at org.junit.runners.Suite.runChild(Suite.java:128)at org.junit.runners.Suite.runChild(Suite.java:24)at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)at at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at at org.junit.runner.JUnitCore.run(JUnitCore.java:157)at org.junit.runner.JUnitCore.run(JUnitCore.java:136)at at 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);
编辑:我找到了合适的解决方案,寻找接受的答案。
答案 0 :(得分:12)
好的,我找到了一个合适的解决方案!在处理使用处理程序的库和apis时,您需要使用 @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);
}
答案 1 :(得分:1)
尝试在模拟线程中调用该行:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btnFacebook.setEnabled(false);
}
}, 100);