Run Test in android studio with UI test cases


I am trying to add UI test cases in my current working app. For that I have added this in app/src/androidtest/java/com.testapp

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

 * Instrumented test, which will execute on an Android device.
 * See [testing documentation](
class ExampleInstrumentedTest {

    fun loginActivityTest() {
// Type text and then press the button.

        // This view is in a different Activity, no need to tell Espresso.
//        onView(withId(

    companion object {
        val STRING_EMAIL  ""
        val STRING_PASSWORD  "test1234"

Added in build.gradle :

// Testing-only dependencies
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
androidTestImplementation 'androidx.test:core-ktx:' + rootProject.coreVersion
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion

testImplementation 'androidx.test:core:' + rootProject.coreVersion;
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
testImplementation 'androidx.test.espresso:espresso-intents:' + rootProject.espressoVersion
testImplementation 'androidx.test.ext:truth:' + rootProject.extTruthVersion

buildToolsVersion  "28.0.3"
androidxLibVersion  "1.0.0"
robolectricVersion  "4.3"
extTruthVersion  '1.3.0-alpha02'
coreVersion  "1.2.1-alpha02"
extJUnitVersion  "1.1.2-alpha02"
runnerVersion  "1.3.0-alpha02"
espressoVersion  "3.3.0-alpha02"

But after editing configuration to run test class only. I am getting error :

    Task :app:compileappnameqaDebugAndroidTestJavaWithJavac FAILED
F:\test_project\Android_new\app\build\generated\source\buildConfig\androidTest\debug\com\appname\test\ error: cannot find symbol
  public static final int appIcon  R.mipmap.ic_launcher;
  symbol:   variable mipmap
  location: class R
F:\test_project\Android_new\app\build\generated\source\buildConfig\androidTest\debug\test\com\appname\test\ error: cannot find symbol
  public static final int splashImage  R.drawable.splash;
  symbol:   variable drawable
  location: class R
2 errors

Can anyone please help me how to know how to run the UI test case in my app?


After Adding line :

@get:Rule val activityRule  activityScenarioRule<LoginActivity>()

Still I was facing same issue.Then I copied those drawable and mipmap files created res/drawable and res/mipmap and pasted those files in folders. Which in result removed the BuildConfig error. Then I started facing error :

Command line is too long. Shorten command line for 'testconfiguration' or also for Android JUnit default Configuration

To overcome this : select option "classpath file" in test Configuration setup, Which will run the test.

Answered By – Apurva Kolapkar

Leave a Comment