代码之家  ›  专栏  ›  技术社区  ›  Roberto Pinheiro

Android Studio中未识别“RobolectrictTestRunner.class”

  •  16
  • Roberto Pinheiro  · 技术社区  · 7 年前

    我试图使用Robolectric来运行单元测试,但Android Studio无法识别上的类:

    @RunWith(RobolectricTestRunner.class)
    

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

    我正在导入依赖项:

    testImplementation "org.robolectric:robolectric:3.5.1"
    

    以及:

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

    它只是不显示导入类的选项。 我应该添加更多我忘记的依赖项吗?

    错误:

     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
    
    4 回复  |  直到 7 年前
        1
  •  39
  •   Roberto Pinheiro    7 年前

    我只需将测试类放入 src \ test 文件夹,而不是将其放入 src \ androidTest 文件夹

        2
  •  7
  •   Fakher Joonas    7 年前

    RobolectricGradleTestRunner 自Robolectric的第3版以来,已弃用。只需使用 @RunWith(RobolectricTestRunner.class) 看看官方 documentation .

        3
  •  7
  •   ManuQiao    5 年前

    通常有两种测试“androidTest”和“test”。

    androidTestImplementation 'org.robolectric:robolectric:4.3.1' ,Robolectric包将仅在“androidTest”类中导入。

    如果添加依赖项,如 testImplementation 'org.robolectric:robolectric:4.3.1' ,它们只能在“测试”类中导入。

        4
  •  7
  •   mobibob    4 年前

    我很幸运地遵循了大部分建议。
    然后我换了一个点(.)双冒号(:)

    @RunWith(RobolectricTestRunner.class)
    

    已更改为。。。

    @RunWith(RobolectricTestRunner::class)