Android:Espresso + Dagger + DaggerMock + Mockito

时间:2019-06-14 07:34:05

标签: android mockito android-espresso dagger-2

我正在尝试在我们的Android项目(使用Dagger)中设置Espresso。 我在使用Mockito时遇到很多麻烦,并且在here中遇到了错误。

这是我的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--methodshttps://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

非常感谢!

0 个答案:

没有答案