AndroidX : No instrumentation registered! Must run under a registering instrumentation

后端 未结 6 1698
长情又很酷
长情又很酷 2020-12-09 14:26

I\'m trying to run a local unit test that depends on the context, and was following this guide: https://developer.android.com/training/testing/unit-testing/local-unit-tests#

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 15:32

    Next things are not mentioned on google test guide, but they are what I found:

    1. androidx.test in case of unit tests is just an interface/api (I don't know what about instrumented tests) and it needs implementations, which is robolectric library. That's why robolectric dependency is also required: testImplementation "org.robolectric:robolectric:{version}"
    2. @RunWith(AndroidJUnit4.class) is required. To get nondeprecated class you need to add: testImplementation "androidx.test.ext:junit:{version}". By the way this dependency has transitive junit4 dependensy.

    Also you can faced with: Failed to create a Robolectric sandbox: Android SDK 29 requires Java 9 (have Java 8) in case you use java 8 and compileSdkVersion 29 or above. Here you can find how to deal with it.

提交回复
热议问题