我正在开发一个应用程序,并且想通过单击登录按钮来测试是否成功登录,从而更改了我的应用程序的登录值,但是我一直得到:
CalledFromWrongThreadException
编辑:添加了堆栈跟踪。我正在使用Android Studio 3.1.3,并且正在虚拟设备(Nexus S Lollipop)上运行测试。
代码:
@Rule
public ActivityTestRule<LoginActivity> loginActivityActivityTestRule =
new ActivityTestRule<LoginActivity>(LoginActivity.class);
private LoginActivity loginActivity = null;
@Before
public void setUp() throws Exception {
loginActivity = loginActivityActivityTestRule.getActivity();
}
@Test
public void checkLoginButton() throws Throwable {
final Button login = loginActivity.findViewById(R.id.login);
loginActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
login.performClick();
}
});
assertTrue(loginActivity.sc.loggedin);
}
@After
public void tearDown() throws Exception {
loginActivity = null;
}
Stacktrace:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:909)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4690)
at android.view.View.invalidateInternal(View.java:11801)
at android.view.View.invalidate(View.java:11737)
at android.widget.TextView.invalidateRegion(TextView.java:4895)
at android.widget.TextView.invalidateCursor(TextView.java:4838)
at android.widget.TextView.spanChange(TextView.java:7774)
at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:9499)
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:981)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:690)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:607)
at android.text.Selection.setSelection(Selection.java:76)
at android.text.Selection.setSelection(Selection.java:87)
at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:318)
at android.widget.TextView.setText(TextView.java:4045)
at android.widget.TextView.setText(TextView.java:3915)
at android.widget.EditText.setText(EditText.java:85)
at android.widget.TextView.setText(TextView.java:3890)
at swp.app.LoginActivityTest.checkLoginButton(LoginActivityTest.java:61)
at 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: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.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.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:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
答案 0 :(得分:1)
由于您使用的是ActivityTestRule
,所以我假设您使用的是Espresso。如果您使用的是Espresso,则应使用Espresso API与视图进行交互,而不是查找视图并调用performClick
()。将您的测试更新为以下内容:
@Test
public void checkLoginButton() throws Throwable {
onViewWithId(R.id.login).perform(click());
assertTrue(loginActivity.sc.loggedin);
}