lundi 9 décembre 2019

Run a test using fragment-testing

I have an Android application I am building and would like to use the fragment-testing libraries to test some fragments in isolation. However, I am struggling to get the tests to execute:

2019-12-09 13:57:03.255 22352-22352/net.redlightning.solo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: net.redlightning.solo, PID: 22352
    java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{net.redlightning.solo.test/androidx.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.AndroidJUnitRunner" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/net.redlightning.solo.test-IhE3r3tw8UMYiQVbxTUcKw==/base.apk", zip file "/data/app/net.redlightning.solo-OBV060XuIVKmHuPoKbpynw==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system/product/lib64]]
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6395)
        at android.app.ActivityThread.access$1300(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.AndroidJUnitRunner" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/net.redlightning.solo.test-IhE3r3tw8UMYiQVbxTUcKw==/base.apk", zip file "/data/app/net.redlightning.solo-OBV060XuIVKmHuPoKbpynw==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6391)
        at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/runner/MonitoringInstrumentation;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
                ... 10 more
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.MonitoringInstrumentation" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/net.redlightning.solo.test-IhE3r3tw8UMYiQVbxTUcKw==/base.apk", zip file "/data/app/net.redlightning.solo-OBV060XuIVKmHuPoKbpynw==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 13 more

Here is my app's gradle.build file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "net.redlightning.solo"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

kapt {
    useBuildCache = false
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core:1.1.0'
    implementation 'androidx.fragment:fragment:1.2.0-rc03'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.preference:preference:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.google.maps.android:android-maps-utils:0.6.2'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.google.android.gms:play-services-places:17.0.0'
    implementation 'com.google.android.gms:play-services-plus:17.0.0'
    implementation 'com.github.deano2390:MaterialShowcaseView:1.3.4'
    implementation 'com.github.zafarkhaja:java-semver:0.9.0'
    implementation 'com.jcraft.jsch:com.springsource.com.jcraft.jsch:0.1.41'
    implementation 'com.mixpanel.android:mixpanel-android:5.6.6'
    implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    implementation 'com.squareup.okio:okio:2.4.1'
    implementation 'com.squareup.okhttp3:okhttp:4.2.2'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.2.2'
    implementation 'io.dronefleet.mavlink:mavlink:1.1.5'
    implementation 'me.grantland:autofittextview:0.2.1'
    implementation 'org.mp4parser:isoparser:1.9.41'
    implementation 'pl.tajchert:waitingdots:0.2.0'
    implementation 'com.jakewharton.timber:timber:4.7.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    debugImplementation 'androidx.fragment:fragment-testing:1.2.0-rc03'
}

apply plugin: 'com.google.gms.google-services'

And here is the test class (simplified), located in androidTest:

package net.redlightning.solo.fragment

import android.content.Context
import android.location.Location
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class PlanningWidgetFragmentTest {
    private var instrumentationContext: Context? = null
    @Before
    fun setUp() {
        instrumentationContext = InstrumentationRegistry.getInstrumentation().targetContext
    }

    @Test
    fun fetchWeatherInformationRequest() {
        val location = Location("flp")
        location.latitude = 43.073051
        location.longitude = -89.401230
        location.altitude = 266.0
        location.bearing = 0.0f

        val scenario = launchFragmentInContainer<PlanningWidgetFragment>()
        scenario.onFragment {
            val result = it.fetchWeatherInformationRequest(location)
            assertNotNull(result)
        }
    }

I have tried with and without the @RunWith(AndroidJUnit4::class) annotation on the test class, I have tried with 1.2.0-rc03 and 1.1.0 versions of androidx.fragment:fragment-testing (and androidx.fragment:fragment so they match) in gradle, I've tried having the import be in debugImplmentation (as the docs and articles from a year ago describe), implementation, and androidTestImplementation. Nothing seems to work.

It is likely worth noting that if I remove fragment-testing from the project, and comment out the lines using it in the test (so it just builds a location it doesn't use) the test runs to completion just fine.

What is the correct setup to allow me to use androidx.fragment:fragment-testing?

Aucun commentaire:

Enregistrer un commentaire