我试图使用 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:org.mockito:mockito-core:4.0.0”,而我有“org.mockito:mockito-core:4.0.0” ”
这就是我解决它的方法,以防有人需要:
首先,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 的选项。否则,祝你好运,我的朋友。