2

I'm trying to add instrumented test using Espresso in my android project.

I've added all necessary libs, besides adding AndroidJUnitRunner as the testInstrumentationRunner:

app -> build.gradle

But for some reason, the imports are not working in instrumented test file:

androidTest file

But the local test file is getting the imports properly:

local test file

I've also added junit as androidTestImplementation, but makes no difference:

androidTestImplementation 'junit:junit:4.12'

This is why I cannot run the test file.

If I run the file anyway, I get this error:

Process finished with exit code 1
Class not found: "com.mcp.shippax.MainActivityEspressoTest"Empty test suite.

I cannot understand why this is happening (numerous grade syncs/invalidate caches/restarts), when the setup is so straight and simple.

I don't remember making any project changes other than converting most of my source files to Kotlin, including the test files. But later I reverted the test files back to java again.

Abu Ruqaiyah
  • 1,516
  • 1
  • 12
  • 20
  • Are you putting your test file in `test` or `androidTest` folder? – Bach Vu Feb 13 '19 at 06:31
  • @BachVu in androidTest obviously :-) https://pasteboard.co/I0VLPBz.png. – Abu Ruqaiyah Feb 13 '19 at 06:35
  • Then it won't work, what you are doing is unit test, create test file in `test` folder – Bach Vu Feb 13 '19 at 06:43
  • Um.. why do u think it's unit test? and what should I do to make it instrumented test? Fyi.. I'm simply trying this: https://github.com/google-developer-training/android-fundamentals-apps-v2/blob/master/TwoActivitiesEspresso/app/src/androidTest/java/com/example/android/twoactivities/ActivityInputOutputTest.java – Abu Ruqaiyah Feb 13 '19 at 06:50
  • Oh my bad, I'm messing it up. Now, from what I'm seeing, you might be missing this `androidTestImplementation org.jetbrains.kotlin:kotlin-test-junit:..` – Bach Vu Feb 13 '19 at 07:59
  • Nope, didn't work. And why should kotlin lib be added. The file is in java, not kotlin. As I mentioned, I converted the test file to kotlin at first, but then reverted back to java again. – Abu Ruqaiyah Feb 13 '19 at 09:09
  • I thought you said the error is when you converted to Kotlin – Bach Vu Feb 13 '19 at 09:13
  • I converted at first, but then reverted back to java, that's what I meant in the last lines. – Abu Ruqaiyah Feb 13 '19 at 09:16
  • Ok, and you said you had it but I want to confirm that you added this line `testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"` right? – Bach Vu Feb 13 '19 at 09:24
  • Yep.. not androidx, but android-> testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" – Abu Ruqaiyah Feb 13 '19 at 11:50

1 Answers1

3

I had the same issue. I've had various build types for dev and prod environments and what helped me, was to specify testBuildType like in this answer https://stackoverflow.com/a/34778780/9736105.

So something like this:

android {

    ...

    testBuildType "<my-build-type>"

    buildTypes {
        <my-build-type> {
            ...  
        }
        ....
    }
}

After I did that and set my build variant to that build type, I was able to resolve all dependecies.