微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Junit5

如何解决使用 Junit5

我必须测试当前位置是否靠近某个特定位置。在我的测试课中,我试图通过设置经度和纬度来创建该特定位置,在应用程序中,这些数据来自后端。在我的测试课中,我手动输入它们。因为我的函数重复了太多次,所以我必须使用 junit5 提供的 ParametizedTest。但是使用 junit5 使我无法 RunWith(RoboElectricTestRunner.class)。相反,我开始使用 mannodermaus 插件通过 junit5 进行 Android 测试。它带来了另一个我无法弄清楚的问题,我的 Location 对象现在返回 null 而不是设置 lon 和 lat

 private fun strings(): Stream<String?>? {
            return Stream.of( "foo","bar")
        }
    
        @ParameterizedTest
        @MethodSource("strings")
        fun updateLocation(input: String)  {      
    
            var lat = 12.494831
            var lon = 83.49899
            val startLocation = Location("start")
            startLocation.latitude = lat
            startLocation.longitude = lon
    
            **println("startLocation = $startLocation") **//returns null****
    }

在它工作正常之前,当我使用 JUnit4 和 @RunWith(RobolectricTestRunner.class) 时。现在我使用 junit5 和 mannodermaus 插件

我的 build.gradle 文件如下所示。

  apply plugin: 'de.mannodermaus.android-junit5'

apply from: rootProject.file('buildscripts/sonar.gradle')

android.buildFeatures.dataBinding = true

dependencies {

    //kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$rootProject.kotlinVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutinesVersion"

    // External libraries
    implementation "com.jakewharton.timber:timber:$rootProject.timberVersion"

    // Android Architecture Components
    api "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycLeversion"
    api "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycLeversion"

    implementation 'joda-time:joda-time:2.10.5'

    //testing libraries
    testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"
    testImplementation "org.junit.jupiter:junit-jupiter-params:5.7.0"
    testCompile "org.robolectric:robolectric:4.3"
    testImplementation "org.mockito:mockito-junit-jupiter:2.19.0"
    androidTestImplementation "androidx.test.ext:junit:$rootProject.extjunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"

}

android {
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }

    testOptions {
        testOptions {
            unitTests.returnDefaultValues = true
            junitPlatform {
                filters {
                    includeTags "slow"
                    excludeTags "integration"
                }
                debugFilters {
                    includeTags "integration"
                }
            }
        }
    }

    kotlinoptions {
        jvmTarget = "1.8"
    }

    //If your minSdkVersion is 24 or higher,then your issue will be resolved on its own,because VectorDrawables have full support starting with this API.
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。