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.
Related
Here is it my sreen:
What settings should I set?
How to avoid deprecated class?
I would like to use espresso, after that try to use UiAutomator, how to set needed necessary settings?
I lost almost whole day.
#RunWith(AndroidJUnit4::class)
#LargeTest
class MainActivityTest {
#Rule
#JvmField
var activityTest = ActivityTestRule(MainActivity::class.java)
fun c(){
assertEquals(1,1)
}
}
GRADLE
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.network"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
"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'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
//okhttp library
implementation("com.squareup.okhttp3:okhttp:4.7.2")
// coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
//gson
implementation 'com.google.code.gson:gson:2.8.6'
//recycler
implementation "androidx.recyclerview:recyclerview:1.1.0"
//picasso
implementation 'com.squareup.picasso:picasso:2.71828'
//broadcast
implementation 'com.android.support:design:26.1.0'
//interceptor
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
}
You've not set the test runner correctly.
This line:
"android.support.test.runner.AndroidJUnitRunner"
should be instead:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
as you are using the new androidx libraries instead of the old support ones.
Without this you'll get this No tests were found errors. Don't forget to add the testInstrumentationRunner keyword.
And add junit to androidTestImplementation also in de gradle file:
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
I have a set of instrumented Android tests which run on an emulated device. I can run them with gradle using gradlew connectedDebugAndroidTest, and I've set up gradle-pitest-plugin like so:
buildscript {
repositories {
google()
jcenter()
mavenLocal()
mavenCentral()
}
configurations.maybeCreate('pitest')
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.google.ar.sceneform:plugin:1.13.0'
classpath 'pl.droidsonroids.gradle:gradle-pitest-plugin:0.2.2'
}
}
apply plugin: 'com.android.application'
apply plugin: 'pl.droidsonroids.pitest'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "my.application.id"
minSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled = true
}
}
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
// Provides ARCore Session and related resources.
implementation 'com.google.ar:core:1.13.0'
// Provides ArFragment, and other Sceneform UX resources:
implementation "com.google.ar.sceneform.ux:sceneform-ux:1.13.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:+"
// AndroidX Test dependencies
// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Assertions
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.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.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.1.0'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation "org.mockito:mockito-android:+"
implementation 'org.pitest:pitest:1.4.5'
}
pitest {
targetClasses = ['class.to.test.*']
threads = 5
outputFormats = ['HTML']
verbose = true
}
When I run gradlew pitest or gradlew pitestDebug, the emulator doesn't start, and only my unit tests run. Specifying the instrumented test class in the pitest config or specifying a different test runner doesn't help. I'm new to Android Studio and configuring mutation testing with gradle, so I'm not sure if I'm missing something simple or this absolutely isn't possible.
Update: it's not possible without a PITest plugin, according to the maintainer of gradle-pitest-plugin.
If you are test unitary your UI without need the instrumented mecanisme add like they said : If you are using alternative Android framework in tests, like Robolectric or UnMock Gradle Plugin, you may want to add excludeMockableAndroidJar to pitest configuration
excludeMockableAndroidJar = true -> For Robolectric
pitest {
targetClasses = ['com.myapp.*']
excludeMockableAndroidJar = true
}
ERROR: Unable to resolve dependency for ':app#debugUnitTest/compileClasspath': Could not download accessibility-test-framework.jar (com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.1)
Show Details
Affected Modules: app
Using Accessibility Test Framework Library to test for common accessibility issues.
This is my build.gradle(Module: App)
apply plugin: 'com.android.application'
repositories {
maven {
url 'https://repo1.maven.org/maven2/'
}
}
android {
compileSdkVersion 26
buildToolsVersion '26.0.3'
defaultConfig {
applicationId "com.pluralsight.candycoded"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
sourceSets.test.resources.srcDirs += ["src/main"]
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.picasso:picasso:2.71828'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'com.github.javaparser:javaparser-core:3.0.1'
//testImplementation "org.mockito:mockito-core:2.+"
testImplementation "org.powermock:powermock-module-junit4:1.7.1"
testImplementation "org.powermock:powermock-api-mockito2:1.7.1"
testImplementation "org.powermock:powermock-core:1.7.1"
testImplementation 'org.robolectric:robolectric:4.3.1'
}
After implement Firebase on my two projects, AppCompatActivity along with import androidx.appcompat.app.AppCompatActivity and import android.support.v7.app.AppCompatActivity are not found.
I've been looking for five hours. And even after removing the implementation it doesn't work.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.company.pc.livium"
minSdkVersion 16
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'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
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.android.support:support-v4:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.anjlab.android.iab.v3:library:1.0.44'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
Go to Refactor -> Migrate to AndroidX… and migrate your project to AndroidX.
AppCompat is deprecated, your build.gradle should look something like this under depenedencies {}:
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
You can try:
1. Build -> Clean project than Build -> Rebuild Project
if that doe not help
2. File -> Invalidate Caches/Restart (Take a few minutes to perform)
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.