Android单元测试 Only the original thread that created a view hierarchy can touch its views.

错误日志:

Only the original thread that created a view hierarchy can touch its views.

原因:

在测试类中设置UI组件的属性。如textView.setText("hello");

分析:

测试类是在工作线程,而不是在UI线程,所以更新UI组件会报错。

Android官方文档:When testing an Activity that has a user interface (UI), the Activity under test runs in the UI thread. However, the test application itself runs in a separate thread in the same process as the application under test. This means that your test app can reference objects from the UI thread, but if it attempts to change properties on those objects or send events to the UI thread, you will usually get a WrongThreadException error.

PS:

JUnit单元测试,不支持线程,会直接开启线程并略过。


你可能感兴趣的:(Android)