[英]Android: Espresso + Dagger + DaggerMock + Mockito
我正在尝试在我们的Android项目(使用Dagger)中设置Espresso。 我和Mockito有很多麻烦,我在这里得到错误。
这是我的app.gradle
一部分:
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:$rootProject.mockitoKotlinVersion"
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:$rootProject.mockitoKotlinVersion"
testImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.25.0'
testImplementation "androidx.test:rules:$rootProject.androidXTestJUnitVersion"
testImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"
androidTestImplementation ('org.mockito:mockito-android:2.25.0') {
exclude group: 'net.bytebuddy', module: 'byte-buddy'
exclude group: 'net.bytebuddy', module: 'byte-buddy-android'
exclude group: 'net.bytebuddy', module: 'byte-buddy-agent'
}
我也有:
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'net.bytebuddy') {
details.useVersion "1.8.22"
}
}
}
我也跟着文章https://github.com/linkedin/dexmaker#mocking-final-classes--methods和https://proandroiddev.com/mocking-androidtest-in-kotlin-51f0a603d500 ,所以我可以能够在仪器测试中模拟我的类。
这是我的第一个测试类:
@RunWith(AndroidJUnit4::class)
class GoalEvaluationEntryFragmentTest {
@get:Rule
var activityRule = ActivityTestRule(GoalEvaluationActivity::class.java)
@get:Rule
val rule = espressoDaggerMockRule()
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
val fragment = GoalEvaluationEntryFragment()
(activityRule.activity as FragmentActivity).supportFragmentManager.beginTransaction()
.replace(R.id.mainContainer, fragment).commit()
}
@Test
fun a() {
}
}
`espressoDaggerMockRule就是这样
fun espressoDaggerMockRule() =
DaggerMock.rule<AppComponent>(
MainModule(),
ServicesModule(app),
ModelsModule(app)
) {
set { component -> app.component = component }
}
val app: MDApplication get() = ApplicationProvider.getApplicationContext<Context>() as MDApplication
非常感谢提前!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.