How to use android.test.mock in Android Studio
Asked Answered
B

3

10

I'm trying to use MockContext in unit tests for Android projects in Android Studio. The problem is, package android.test.* is not visible in the project.

I'm not sure what should I add to Gradle in order to import it. I tried com.android.support.test:rules:1.0.2 and androidx.test:rules:1.1.1 (one of the suggestions made by IDE), but that's not the one I'm looking for.

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myApp"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.8'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    testImplementation 'com.android.support.test:rules:1.0.2'
}

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

Error message when compiling:

error: package android.test.mock does not exist

What should I add to gradle to have access to android.test packages?

Bedfordshire answered 5/5, 2019 at 21:34 Comment(3)
You used implementation for that dependency. Try testImplementation.Constrained
@Constrained That doesn't help, but thanks for catching that :) Edited.Bedfordshire
Had the same problem here! Strangely, Siena's answer wasn't working for my library module's test... and then adding useLibrary 'android.test.mock' in my application module's build.gradle FIXED the failing import!!! In my library module test. Weird...Kingery
G
14

Actually, you can access android.test.mock from your unit tests and gain access to the MockContext class. For AndroidX projects, you can add the following in your app module's build.gradle:

android {
    ...
    useLibrary 'android.test.mock'
}

Source: https://developer.android.com/training/testing/set-up-project

Gusella answered 31/7, 2019 at 18:24 Comment(2)
Explanation of useLibrary: This directive adds the library to the classpath when building but does not add the library to the APK.Deodorant
Not helpful as it dont work I add this line and still cannot access MockContext also didnt find anything helpfull in documentation.Djebel
C
2

That class is a framework class, not from a library. You can tell by the package name: anything in android. (instead of com.android. or androidx.) is a framework class. Hence, you could only use that for instrumented tests, not unit tests.

You can either create custom mocks (e.g., with Mockito) or use Robolectric, depending on what you're trying to do.

Constrained answered 5/5, 2019 at 21:42 Comment(3)
Ah, I see. I was hoping for an easy to use mock. But why does android.content.Context work in unit tests? It also seems to start with android. only.Bedfordshire
@Yksisarvinen: That depends on what you mean by "work". You won't be able to create or get a Context in a unit test, outside of mocks. But you do raise a good point: I am not certain why your import is not working.Constrained
Recent version of mockk framework fails when mocking android Context and I cant use robolectricDjebel
E
2

The accepted answer suggests to use android.test.mock. That can work, in the sense that you can run tests with it. However, it is a kind of overkill, as it means you will be doing instrumented test instead of unit tests. That brings with it all the implications and overhead, etc., of doing instrumented tests, whereas if you just need a context object for unit tests, it is not necessary nor advisable to use android.test.mock.

Furthermore, the question above actually is asking for some kind of Context object to use for unit tests. And Google's developer documentation does address this question, with sample code that imports android.content.Context and then mocks it with Mockito.

But isn't android.content.Context a framework class? Then how can it be used in unit tests rather than instrumented tests? As can be seen in developer reference for android.content.Context, it is an abstract class. Hence it is suitable for simple mocking with Mockito.

Ecdysiast answered 2/5, 2021 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.