AndroidJUnit4.class is deprecated: How to use androidx.test.ext.junit.runners.AndroidJUnit4?

前端 未结 8 1614
傲寒
傲寒 2020-12-04 08:04

For my instrumentation tests I was using

@RunWith(AndroidJUnit4.class)

from

import androidx.test.runner.AndroidJUnit4;


        
相关标签:
8条回答
  • 2020-12-04 08:47

    If you've tried @MarcelGangwisch's solution and your build fails saying it can't find the resource AND you also cleaned/rebuilt your project and it still doesn't work, try this: (based also on @KrzysztofDziuba's solution)

    In your gradle file where you changed the dependency, make sure you are adding it as the type you need, ie.:

    For UI tests:

    androidTestImplementation 'androidx.test.ext:junit:1.1.0'

    For Unit tests:

    testImplementation 'androidx.test.ext:junit:1.1.0'

    In my instance I added it as both and now it works.

    0 讨论(0)
  • 2020-12-04 08:53
    1. Include these lines in build.gradle app module.

      testImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'

    2. In the test class replace your test runner to

      import androidx.test.ext.junit.runners.AndroidJUnit4;

    3. If InstrumentationRegistry.getTargetContext() is deprecated use InstrumentationRegistry from androidx.test.platform.app like this

      InstrumentationRegistry.getInstrumentation().getTargetContext()

    0 讨论(0)
提交回复
热议问题