Android Instrumentation Testing - UI Thread Issues

前端 未结 5 1012
走了就别回头了
走了就别回头了 2020-12-30 22:16

I am trying to write an Instrumentation Test for my Android app.

I\'m running into some weird threading issues and I can\'t seem to find a solution.

My

5条回答
  •  再見小時候
    2020-12-30 22:23

    Those instrumentation tests run inside their own app. This also means, they run in their own thread.

    You must think of your instrumentation as something you install alongside your actual app, so your possible interactions are 'limited'.

    You need to call all view methods from the UIThread / main thread of the application, so calling activity.updateDetails(workOrder); from your instrumentation thread is not the application main thread. This is why the exception is thrown.

    You can just run the code you need to test on your main thread like you would do if you were calling it inside your app from a different thread by using

    activity.runOnUiThread(new Runnable() {
        public void run() {
            activity.updateDetails(workOrder);
        }
    }
    

    With this running your test should work.

    The illegal state exception you are receiving seems to be because of your interaction with the rule. The documentation states

    Note that instrumentation methods may not be used when this annotation is present.

    If you start / get your activity in @Before it should also work.

提交回复
热议问题