Wednesday 1 September 2021

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
    }
}


from Location object return null while testing with Junit5

No comments:

Post a Comment