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

huangapple go评论69阅读模式
英文:

@RunWith(MockitoJUnitRunner::class) does not recognize the mockito library [solved]

问题

我试图使用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"了。

英文:

I was trying to use Mockito to have a context for my test, but it it said that I needed to add the library "Gradle: org.mockito:mockito-core:4.0.0", which I already had. I was looking at this page,

Both MockitoJUnitRunner and Mock appeared in red.

@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

    (...)
}

These were my imports:

    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"

I put "Gradle:" before the import because the tip to fix the error said to import "Gradle: org.mockito:mockito-core:4.0.0" and I had "org.mockito:mockito-core:4.0.0"

答案1

得分: 1

这是我解决问题的方法,如果有人需要的话:

首先,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的选项。否则,祝你好运,我的朋友。

英文:

This is how I solved it, in case anyone needs it:

First of all, Android Studio wasn't recognizing my test directory; I needed to add the directory app/src/test/java/com/example/project and add there my tests. Then I changed all the androidTestImplementation to testImplementation AND added the library "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"

Now, it should appear the option to import org.mockito.junit.MockitoJUnitRunner in the test file. Otherwise, good luck my friend.

huangapple
  • 本文由 发表于 2023年7月12日 20:48:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定