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?
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'
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.
Related
I keep getting the Cannot resolve class android.support.v4.widget.DrawerLayout error even after adding both the DrawerLayout androidx.drawerlayout:drawerlayout:1.1.1 and Material com.google.android.material:material:1.0.0 implementations in my build.gradle file.
I have tried adding other implementations, such as com.android.support:support-compat:28.0.0 and com.android.support:design:25.0.0, but the error remains and the app won't start.
Any idea on how to fix this?
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "edu.ktu.birthdaycalendar"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.drawerlayout:drawerlayout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def room_version = "2.2.5"
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
//lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
androidTestImplementation "androidx.arch.core:core-testing:$arch_version"
implementation "com.google.android.material:material:1.0.0"
/*implementation "com.android.support:support-compat:28.0.0"
implementation "com.android.support:design:25.0.0"
implementation "com.android.support:appcompat-v7:27.1.1"
implementation "com.android.support:support-v4:27.1.1"
implementation "com.android.support:design:27.1.1"
//implementation 'com.android.support.constraint:constraint-layout:1.1.0' */
}
You've imported the wrong version of DrawerLayout. android.support.v4.widget.DrawerLayout
It should be import androidx.drawerlayout.widget.DrawerLayout
Check the class mapping between the support libraries and androidx libraries:
android.support.v4.widget.DrawerLayout -> androidx.drawerlayout.widget.DrawerLayout
Use in your layout and in your code androidx.drawerlayout.widget.DrawerLayout.
I have successfully migrated to Androidx, everything seems to work. Now I'm stuck in migrating (or basically creating tests) for my project.
I added those dependencies to my project:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
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:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
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-identity:17.0.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'joda-time:joda-time:2.9.9'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'ch.acra:acra:4.6.1'
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
implementation 'com.diegocarloslima:fgelv:0.1.2'
implementation 'com.luckycatlabs:SunriseSunsetCalculator:1.1'
implementation project(':cp963_cpik_lib')
I created two paths for two tests (one is for test which works fine and Android studio recognize the #Test annotation of junit class, the other is for androidTest which Android studio does not recognize any of the classes). Both test and androidTest are green and have the same package name as my src folder.
This is the test file (works just fine):
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* #see Testing documentation
*/
public class ExampleUnitTest {
#Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
This is androidTest that I'm not sure why Android Studio is not able to import any of the classes
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("xxxxxx", appContext.getPackageName());
}
}
The classes that I mean are #RunWith, AndroidJUnit4, #Test, InstrumentationRegistry, assertEquals.
I basically used the generated tests that came with a new project. Do I need somehow to configure Android Studio to use junit in androidTest? or is it something to do with the copilot library that I'm using?
Thank you
Edit 1
I added the previous test that was in my project:
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
It's an old project which I'm trying to use more up to date dependencies and unit testing.
Edit 2
This is my build.gradle.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "xxx.xxx.xxx"
minSdkVersion 19 // 17 = 4.2 Jelly Bean, 18 = 4.3 jelly bean, 19 = 4.4 kitkat
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 19 // 21 = 5.1 Lollipop
versionCode 65
versionName "1.65"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
signingConfig signingConfigs.config
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
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:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
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-identity:17.0.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'joda-time:joda-time:2.9.9'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'ch.acra:acra:4.6.1'
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
implementation 'com.diegocarloslima:fgelv:0.1.2'
implementation 'com.luckycatlabs:SunriseSunsetCalculator:1.1'
implementation project(':cp963_cpik_lib')
androidTestImplementation 'junit:junit:4.12' // I tried with this and without but I got the same result.
}
I was just learning how to create tests using Robolectric. I was just writing my first Test as following:
#RunWith(RobolectricTestRunner::class)
class MainActivityTest {
lateinit var mainActivity: MainActivity
#Before
fun setUp() {
mainActivity = setupActivity(MainActivity::class.java)
}
}
But here it shows that setupActivity is deprecated and i should use something called ActivityScenario. So i tried adding dependencies for it as following:
Module level build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.unittesting"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:4.3.1'
// Core library
androidTestImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.2.0'
// The following Espresso dependency can be either "implementation"
// or "androidTestImplementation", depending on whether you want the
// dependency to appear on your APK's compile classpath or the test APK
// classpath.
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.2.0'
}
Still i am unable to resolve the class ActivityScenario. I don't know what i am missing.
It seems like kotlinx-coroutines-test dependency is not working for me as I can't access members of the dependency like TestCoroutineDispatcher, setMain(), resetMain() etc. I was following this doc but can't access the members despite adding the gradle dependency. I tried rebuilding the project and invalidating the cache and restart but nothing seems to work. I have also tried doing androidExtensions {experimental = true} but still no luck.
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.android.kotlincoroutines"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
androidExtensions {
experimental = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// ViewModel and LiveData
def lifecycle_version = '2.0.0-beta01'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha02"
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
// Room for database
def room_version = '2.0.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
def work_version = "1.0.0-rc01"
implementation "android.arch.work:work-runtime:$work_version"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation "com.google.truth:truth:0.42"
androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-M1'
}
Where am I going wrong here?
replacing testImplementation with implementation did the trick for me.
Change your library version to 1.3.2 instead of 1.3.0-M1 like so:
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
And be sure that your tests are unit test in the test folder, not instrumented test in androidTest folder.
just add :-
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
Source :- Here yigit have given solution link and ssems like working .
Try adding core as a dependency on your test. It solved the problem for me.
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
The solution from #Squti worked for me, namely moving the class trying to import the TestCoroutineDispatcher from a package in the src directory to a package in the test directory.
I am using Retrofit, Dagger and Glide for my app. After migrating to androidX, kapt works perfectly with Dagger and Glide annotation processors. The projects build and syncs successfully, however, when I run the app, I get Kotlin compiler exceptions in my ApiService interface, where I use Retrofit annotations
ApiService.kt
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
interface ApiService {
#GET("/popular")
fun getPopular(#Query("lang") lang: String = LANGUAGE): Call<...>
...
companion object {
val STATUS_OK = "ok"
var LANGUAGE = "en"
fun create(): ApiService {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(...)
.build()
return retrofit.create(ApiService::class.java);
}
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 'android-P'
buildToolsVersion '28.0.0-rc2'
defaultConfig {
applicationId "..."
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.android:flexbox:0.3.2'
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0-alpha1"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1"
implementation 'com.github.rubensousa:gravitysnaphelper:1.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.github.bumptech.glide:glide:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'com.google.android.material:material:1.0.0-alpha1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation 'androidx.cardview:cardview:1.0.0-alpha1'
implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
The exception:
NonExistentClass cannot be converted to Annotation #error.NonExistentClass()
I tried using this according to docs
kapt {
correctErrorTypes = true
}
But it led to another exception
java.lang.ClassCastException: com.sun.tools.javac.code.Type$ErrorType cannot be cast to com.sun.tools.javac.code.Type$ArrayType
As I think, the first exception occurs because after updating gradle and android studio with kapt plugin, it suddenly started declaring unknown Retrofit Java annotations with NonExistentClass (there isn't any kapt or annotationProcessor for Retrofit, as you can see in build.gradle). I'm clueless as to what does the second error mean.
I found that setting android.enableJetifier to false in gradle.properties fixes the compilation error. I've filed a bug in the Issue Tracker.