Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

android - Location object return null while testing with Junit5

I have to test if current Location is near some specific location. In my test class Im trying to create that specific location by setting longitude and latitude, In App these data is coming from backend. In my test class Im entering them manually. Because my functions are repeating too many times I have to use ParametizedTest offered by Junit5. But using Junit5 made for me impossible to RunWith(RoboElectricTestRunner.class). Instead I start using mannodermaus plugin for Android testing with Junit5. It brought another problem that I cant figure out, my Location object returns now null instead of setting lon and 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****
    }

Before it was working fine, when I used JUnit4 and @RunWith(RobolectricTestRunner.class). Now I use Junit5 with mannodermaus plugin.

my build.gradle file looks like this.

  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
    }
}
question from:https://stackoverflow.com/questions/65848958/location-object-return-null-while-testing-with-junit5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...