Unable to Set Up Instrumentation Testing Android Studio

Refresh

April 2019

Views

230 time

1

I have a project that doesn't't have instrumentations tests set up, so I am following Android's own instructions but, as usual, it's garbage and doesn't work... here are the instructions

After adding

useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'

in the app-level Gradle file, I attempted to sync the project but get the errors:

Unable to find optional library: android.test.base
Unable to find optional library: android.test.mock
Unable to find optional library: android.test.runner

If I followed their instructions, what could I possibly be missing?

Here is what my files look like:

Project Gradle

buildscript {
    apply from: 'versions.gradle'
    addRepos(repositories)
    dependencies {
        classpath deps.android_gradle_plugin
        classpath deps.kotlin.plugin
        classpath deps.kotlin.allopen
        classpath deps.navigation.safe_args_plugin
        classpath 'com.google.gms:google-services:4.0.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    repositories {
        jcenter()
        google()
    }
}

allprojects {
    addRepos(repositories)
}


task clean(type: Delete) {
    delete rootProject.buildDir
}

App Gradle

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

apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'com.google.gms.google-services'

allOpen {
    // allows mocking for classes w/o directly opening them for release builds
    annotation 'com.devetry.ytp.testing.OpenClass'
}
android {
    compileSdkVersion build_versions.target_sdk
    buildToolsVersion build_versions.build_tools
    defaultConfig {
        applicationId "com.devetry.ytp"
        minSdkVersion build_versions.min_sdk
        targetSdkVersion build_versions.target_sdk
        versionCode 29
        versionName "2.0.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        signingConfig signingConfigs.config
    }
    buildTypes {
        debug { }
        release { }
    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        androidTest.java.srcDirs += "src/test-common/java"
        test.java.srcDirs += "src/test-common/java"
    }
    lintOptions {
        lintConfig rootProject.file('lint.xml')
    }
    productFlavors {
    }

    useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
}

jacoco {
    toolVersion = "0.7.4+"
}

dependencies {
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.11.0'
    implementation deps.okhttp_logging_interceptor
    implementation deps.support.app_compat
    implementation deps.support.recyclerview
    implementation deps.support.cardview
    implementation deps.support.design
    implementation deps.navigation.fragment_ktx
    implementation deps.room.runtime
    implementation deps.lifecycle.runtime
    implementation deps.lifecycle.extensions
    implementation deps.lifecycle.java8
    implementation deps.retrofit.runtime
    implementation deps.retrofit.gson
    implementation deps.glide.runtime
    implementation 'com.android.support:support-v4:27.1.1'
    kapt deps.glide.compiler
    implementation deps.dagger.runtime
    implementation deps.dagger.android
    implementation deps.dagger.android_support
    implementation deps.constraint_layout
    implementation deps.kotlin.stdlib
    implementation deps.timber
    kapt deps.dagger.android_support_compiler
    kapt deps.dagger.compiler
    kapt deps.room.compiler
    kapt deps.lifecycle.compiler
    testImplementation deps.junit
    testImplementation deps.mock_web_server
    testImplementation(deps.arch_core.testing, {
        exclude group: 'com.android.support', module: 'support-compat'
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-core-utils'
    })
    testImplementation deps.mockito.core
    testImplementation deps.mockito_kotlin
    androidTestImplementation deps.navigation.testing_ktx
    androidTestImplementation deps.support.app_compat
    androidTestImplementation deps.support.recyclerview
    androidTestImplementation deps.support.cardview
    androidTestImplementation deps.support.design
    androidTestImplementation(deps.espresso.core, {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    })
    androidTestImplementation(deps.espresso.contrib, {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    })
    androidTestImplementation(deps.arch_core.testing, {
    })
    androidTestImplementation deps.dexmaker
    androidTestImplementation deps.mockito.core, { exclude group: 'net.bytebuddy' }
    implementation 'com.android.support:appcompat-v7:27.0.3'
    implementation 'com.android.support:design:27.0.3'
    implementation 'com.google.firebase:firebase-messaging:17.3.3'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1"

    androidTestImplementation 'junit:junit:4.12'

    androidTestImplementation 'androidx.test:core:1.0.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'

    androidTestImplementation 'androidx.test.ext:junit:1.0.0'
    androidTestImplementation 'androidx.test.ext:truth:1.0.0'
    androidTestImplementation 'com.google.truth:truth:0.42'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}

versions.gradle

ext.deps = [:]
def versions = [:]
versions.arch_core = "1.1.1"
versions.room = "1.1.1"
versions.lifecycle = "1.1.1"
versions.support = "27.1.1"
versions.dagger = "2.15"
versions.junit = "4.12"
versions.espresso = "3.0.1"
versions.retrofit = "2.3.0"
//versions.okhttp_logging_interceptor = "3.9.0"
versions.mockwebserver = "3.8.1"
versions.apache_commons = "2.5"
versions.mockito = "2.7.19"
versions.mockito_all = "1.10.19"
versions.mockito_kotlin = "2.0.0-RC1"
versions.dexmaker = "2.2.0"
versions.constraint_layout = "1.0.2"
versions.glide = "4.7.1"
versions.timber = "4.5.1"
versions.android_gradle_plugin = '3.3.0-alpha07'//"3.1.2"
versions.rxjava2 = "2.1.3"
versions.rx_android = "2.0.1"
versions.atsl_runner = "1.0.1"
versions.atsl_rules = "1.0.1"
versions.hamcrest = "1.3"
versions.kotlin = "1.3.0"
versions.paging = "1.0.0"
versions.work = "1.0.0-alpha03"
versions.navigation = "1.0.0-alpha01"
versions.okhttp = "3.11.0"

def deps = [:]

def support = [:]
support.annotations = "com.android.support:support-annotations:$versions.support"
support.app_compat = "com.android.support:appcompat-v7:$versions.support"
support.recyclerview = "com.android.support:recyclerview-v7:$versions.support"
support.cardview = "com.android.support:cardview-v7:$versions.support"
support.design = "com.android.support:design:$versions.support"
support.v4 = "com.android.support:support-v4:$versions.support"
support.core_utils = "com.android.support:support-core-utils:$versions.support"
deps.support = support

def room = [:]
room.runtime = "android.arch.persistence.room:runtime:$versions.room"
room.compiler = "android.arch.persistence.room:compiler:$versions.room"
room.rxjava2 = "android.arch.persistence.room:rxjava2:$versions.room"
room.testing = "android.arch.persistence.room:testing:$versions.room"
deps.room = room

def lifecycle = [:]
lifecycle.runtime = "android.arch.lifecycle:runtime:$versions.lifecycle"
lifecycle.extensions = "android.arch.lifecycle:extensions:$versions.lifecycle"
lifecycle.java8 = "android.arch.lifecycle:common-java8:$versions.lifecycle"
lifecycle.compiler = "android.arch.lifecycle:compiler:$versions.lifecycle"
lifecycle.reactivestreams = "android.arch.lifecycle:reactivestreams:$versions.lifecycle"
deps.lifecycle = lifecycle

def arch_core = [:]
arch_core.testing = "android.arch.core:core-testing:$versions.arch_core"
deps.arch_core = arch_core

def retrofit = [:]
retrofit.runtime = "com.squareup.retrofit2:retrofit:$versions.retrofit"
retrofit.gson = "com.squareup.retrofit2:converter-gson:$versions.retrofit"
retrofit.mock = "com.squareup.retrofit2:retrofit-mock:$versions.retrofit"
retrofit.rxJava2CallAdapter = "com.squareup.retrofit2:adapter-rxjava2:$versions.retrofit"
deps.retrofit = retrofit
deps.okhttp_logging_interceptor = "com.squareup.okhttp3:logging-interceptor:${versions.okhttp}"
deps.okhttp_urlconnection = "com.squareup.okhttp3:okhttp-urlconnection:${versions.okhttp}"

def dagger = [:]
dagger.runtime = "com.google.dagger:dagger:$versions.dagger"
dagger.android = "com.google.dagger:dagger-android:$versions.dagger"
dagger.android_support = "com.google.dagger:dagger-android-support:$versions.dagger"
dagger.compiler = "com.google.dagger:dagger-compiler:$versions.dagger"
dagger.android_support_compiler = "com.google.dagger:dagger-android-processor:$versions.dagger"

deps.dagger = dagger

def espresso = [:]
espresso.core = "com.android.support.test.espresso:espresso-core:$versions.espresso"
espresso.contrib = "com.android.support.test.espresso:espresso-contrib:$versions.espresso"
espresso.intents = "com.android.support.test.espresso:espresso-intents:$versions.espresso"
deps.espresso = espresso

def mockito = [:]
mockito.core = "org.mockito:mockito-core:$versions.mockito"
mockito.all = "org.mockito:mockito-all:$versions.mockito_all"
deps.mockito = mockito

deps.mockito_kotlin = "com.nhaarman.mockitokotlin2:mockito-kotlin:$versions.mockito_kotlin"

def kotlin = [:]
kotlin.stdlib = "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin" //$versions.kotlin"
kotlin.test = "org.jetbrains.kotlin:kotlin-test-junit:$versions.kotlin"
kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"

deps.kotlin = kotlin
deps.paging = "android.arch.paging:runtime:$versions.paging"

def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
glide.compiler = "com.github.bumptech.glide:compiler:$versions.glide"
deps.glide = glide
deps.dexmaker = "com.linkedin.dexmaker:dexmaker-mockito:$versions.dexmaker"
deps.constraint_layout = "com.android.support.constraint:constraint-layout:$versions.constraint_layout"
deps.timber = "com.jakewharton.timber:timber:$versions.timber"
deps.junit = "junit:junit:$versions.junit"
deps.mock_web_server = "com.squareup.okhttp3:mockwebserver:$versions.mockwebserver"
deps.rxjava2 = "io.reactivex.rxjava2:rxjava:$versions.rxjava2"
deps.rx_android = "io.reactivex.rxjava2:rxandroid:$versions.rx_android"
deps.hamcrest = "org.hamcrest:hamcrest-all:$versions.hamcrest"
deps.android_gradle_plugin = "com.android.tools.build:gradle:$versions.android_gradle_plugin"
ext.deps = deps

def build_versions = [:]
build_versions.min_sdk = 23
build_versions.target_sdk = 27
build_versions.build_tools = "27.0.3"
ext.build_versions = build_versions

def work = [:]
work.runtime = "android.arch.work:work-runtime:$versions.work"
work.testing = "android.arch.work:work-testing:$versions.work"
work.firebase = "android.arch.work:work-firebase:$versions.work"
work.runtime_ktx = "android.arch.work:work-runtime-ktx:$versions.work"
deps.work = work

def navigation = [:]
navigation.runtime = "android.arch.navigation:navigation-runtime:$versions.navigation"
navigation.runtime_ktx = "android.arch.navigation:navigation-runtime-ktx:$versions.navigation"
navigation.fragment = "android.arch.navigation:navigation-fragment:$versions.navigation"
navigation.fragment_ktx = "android.arch.navigation:navigation-fragment-ktx:$versions.navigation"
navigation.safe_args_plugin = "android.arch.navigation:navigation-safe-args-gradle-plugin:$versions.navigation"
navigation.testing_ktx = "android.arch.navigation:navigation-testing-ktx:$versions.navigation"
deps.navigation = navigation

ext.deps = deps

def addRepos(RepositoryHandler handler) {
    handler.google()
    handler.jcenter()
    handler.maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

ext.addRepos = this.&addRepos

AndroidManifest

<!--
  ~ Copyright (C) 2017 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.devetry.ytp">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name=".YTPApp"
        android:windowSoftInputMode="adjustPan"
        android:allowBackup="false"
        android:icon="@drawable/ic_logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <uses-library android:name="android.test.runner"
            android:required="false" />
        <uses-library android:name="android.test.base"
            android:required="false" />
        <uses-library android:name="android.test.mock"
            android:required="false" />

        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="com.devetry.ytp.START" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">

        </activity>
        <activity android:name=".auth.AuthActivity" />
        <service android:name=".FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>

0 answers