Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference ActivityTestRule for AndroidX

I'm trying to test my UI via instrumentation test, with androidX espresso library. In my grade I have:

apply plugin: 'com.android.application'  apply plugin: 'kotlin-android'  apply plugin: 'kotlin-android-extensions' apply plugin: "kotlin-kapt"  android {     compileSdkVersion 28     defaultConfig {         applicationId "it.zehus.mybike"         minSdkVersion 21         targetSdkVersion 28         versionCode 1         versionName "1.0"         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"         android.defaultConfig.manifestPlaceholders = ['appAuthRedirectScheme': 'net.openid.appauthdemo']     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'         }     } }  dependencies {     implementation fileTree(dir: 'libs', include: ['*.jar'])      def lifecycle_version = "2.0.0"     // ViewModel and LiveData     implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"      // room persistence library     def room_version = "2.1.0-alpha04"     implementation "androidx.room:room-runtime:$room_version"     kapt "androidx.room:room-compiler:$room_version" // use kapt for Kotlin     // optional - Coroutines support for Room     implementation "androidx.room:room-coroutines:$room_version"      implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"     implementation 'androidx.appcompat:appcompat:1.0.2'     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'     implementation 'com.google.android.material:material:1.0.0'      // Espresso     androidTestImplementation 'androidx.test:runner:1.1.0'     androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'     implementation project(path: ':remotemanager')     implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.21"      androidTestImplementation 'androidx.test.ext:junit:1.1.0'     // required if you want to use Mockito for unit tests     testImplementation 'org.mockito:mockito-core:2.24.5'     // required if you want to use Mockito for Android tests     androidTestImplementation 'org.mockito:mockito-android:2.24.5'     // Bluetooth sdk     implementation project(path: ':bluetoothmanager')     // Custom views     implementation project(path: ':customviews')   } 

As specified in the documentation I'm attempting to import ActivityTestRule class in my test, however the reference is unresolved.

import androidx.test.filters.LargeTest import androidx.test.runner.AndroidJUnit4 import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith // unresolved reference here import androidx.test.rule.ActivityTestRule import it.zehus.mybike.ui.ride.RideActivity   /**  * Instrumented test, which will execute on an Android device.  *  * See [testing documentation](http://d.android.com/tools/testing).  */ // deprecated here @RunWith(AndroidJUnit4::class) @LargeTest class FragmentDashboardUiTest {     @get:Rule // unresolved reference here     val activityRule = ActivityTestRule(RideActivity::class.java)      @Test     fun myClassMethod_ReturnsTrue() {  } } 

Am I doing something wrong or there's a problem within AndroidX testing libraries?

like image 348
Nicola Gallazzi Avatar asked Feb 21 '19 14:02

Nicola Gallazzi


People also ask

What is AndroidX test?

AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests. For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests.

What is activity test rule?

public ActivityTestRule (Class<T> activityClass, boolean initialTouchMode) Similar to ActivityTestRule but defaults to launch the activity under test once per Test method. It is launched before the first Before method, and terminated after the last After method.

What is rule in Unit testing Android?

The rule launches the chosen activity before each test annotated with @Test , as well as before any method annotated with @Before . The rule terminates the activity after the test completes and all methods annotated with @After finish.


2 Answers

I found out from this documentation page that class ActivityTestRule stays under androidx.test.rule in AndroidX. In order to import the package, I simply added:

 androidTestImplementation 'androidx.test:rules:1.2.0' 

to my gradle.

To sum up my gradle now contains:

// Espresso // Core library androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' // AndroidJUnitRunner and JUnit Rules androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:rules:1.2.0' 
like image 50
Nicola Gallazzi Avatar answered Sep 17 '22 00:09

Nicola Gallazzi


The class is under package: androidx.test.ext.junit.rules;

So obviously the most important dependency is:

androidTestImplementation 'androidx.test.ext:junit:1.1.2'

I am wondering why the other answer is uplifted so much, but it doesn't work for me at all.

Hope this helps others like me.

like image 28
bhw1899 Avatar answered Sep 18 '22 00:09

bhw1899