@RunWith(MockitoJUnitRunner::class)无法识别mockito库[已解决]

q3qa4bjr  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(223)

我试图使用Mockito为我的测试提供上下文,但它说我需要添加库“Gradle:org.mockito:mockito-core:4.0.0”,我已经有了。我在看这一页,
MockitoJUnitRunner和Mock都出现在红色中。

@RunWith(MockitoJUnitRunner::class)
class NoticesRepositoryTest {

    private lateinit var noticesRepository: NoticesRepository
    private lateinit var companiesDao: CompaniesDao
    private lateinit var token: String

    @Mock
    private lateinit var context: Context

    (...)
}

字符串
这些是我的进口:

implementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:4.12'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2"
    testImplementation "Gradle: org.mockito:mockito-core:4.0.0"
    testImplementation "org.mockito:mockito-inline:4.0.0"
    testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"


我把“Gradle:“放在导入之前,因为修复错误的提示说要导入“Gradle:mockito:mockito-core:4.0.0”,我有“org.mockito:mockito-core:4.0.0”

s5a0g9ez

s5a0g9ez1#

这是我解决它的方法,如果有人需要它:
首先,Android Studio无法识别我的测试目录;我需要添加目录app/src/test/java/com/example/project并在那里添加我的测试。然后我将所有androidTestImplementation更改为testImplementation,并添加了库“io.mockk:mockk:4.0.0”:

testImplementation 'androidx.test.ext:junit:4.12'
    testImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.2'
    testImplementation "org.mockito:mockito-core:4.0.0"
    testImplementation "org.mockito:mockito-inline:4.0.0"
    testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
    testImplementation "io.mockk:mockk:4.0.0"

字符串
现在,应该会出现在测试文件中导入org.mockito.junit.MockitoJUnitRunner的选项。否则祝你好运我的朋友。

相关问题