“RobolectricTestRunner.class” not recognized in Android Studio

不想你离开。 提交于 2020-12-30 05:36:02

问题


I'm trying to use Robolectric to run unit tests but the Android Studio is not recognizing the class on:

@RunWith(RobolectricTestRunner.class)

Details:

classpath 'com.android.tools.build:gradle:3.0.0'

And I'm importing dependency:

testImplementation "org.robolectric:robolectric:3.5.1"

And:

android {
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

It simply does not appear option to import the class. Should I add any more dependencies I'm forgetting?

Error:

 error: package org.robolectric does not exist
 error: package org.robolectric does not exist
 error: package org.robolectric.shadows does not exist
 error: cannot find symbol class RobolectricTestRunner
 error: cannot find symbol variable Robolectric
 error: cannot find symbol variable ShadowApplication

回答1:


I solved the issue just by putting the test class inside the src \ test folder instead of putting it in thesrc \ androidTest folder




回答2:


RobolectricGradleTestRunner is deprecated since version 3 of Robolectric. Just use @RunWith(RobolectricTestRunner.class) Take a look at official documentation.




回答3:


Usually there will be 2 kinds of tests 'androidTest' and 'test'.

If you add dependency like androidTestImplementation 'org.robolectric:robolectric:4.3.1', Robolectric package will only be imported in 'androidTest' classes.

If you add dependency like testImplementation 'org.robolectric:robolectric:4.3.1', they can only be imported in 'test' classes.



来源:https://stackoverflow.com/questions/47284536/robolectrictestrunner-class-not-recognized-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!