0

Here is it my sreen:

https://imgur.com/a/L98fHex

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'
}
jeprubio
  • 17,312
  • 5
  • 45
  • 56
Young wizzard
  • 41
  • 1
  • 8

1 Answers1

1

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'
jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • I set "android.support.test.runner.AndroidJUnitRunner", And it throws java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present. Also in test class @RunWith(AndroidJUnit4::class) glows as deprecated – Young wizzard Jun 25 '20 at 08:16
  • Add junit4 to `androidTestImplementation` also in the gradle file and not only in `testImplementation`. Then, once it works, check this for the deprecation error: https://stackoverflow.com/questions/52776716/androidjunit4-class-is-deprecated-how-to-use-androidx-test-ext-junit-runners-an/52776938 – jeprubio Jun 25 '20 at 08:18
  • thank you for responding! I set androidTestImplementation 'junit:junit:4.13' – Young wizzard Jun 25 '20 at 08:25
  • I tried to start test, but it shows java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present this is little strange :( – Young wizzard Jun 25 '20 at 08:27
  • I set androidTestImplementation 'androidx.test.ext:junit:1.1.1' It doesn't glows as deprecated. But shows java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present. In this reference that You sent to me, there is something like: Change test class to AndroidJUnit4ClassRunner from AndroidJUnit4 – Young wizzard Jun 25 '20 at 08:32
  • Maybe you have the old import of junit4 in the test files. With this last one it should be: `import androidx.test.ext.junit.runners.AndroidJUnit4`. If this doesn't work add the imports of the test file to your question and I'll have a look at it all – jeprubio Jun 25 '20 at 08:36
  • that was right. but still java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present – Young wizzard Jun 25 '20 at 08:40
  • Check this also https://stackoverflow.com/questions/51832349/type-android-junit4-not-present-exception (second most voted answer has the details) – jeprubio Jun 25 '20 at 08:42
  • https://imgur.com/a/uMGUNmg When I delete it, and just reopen Run -> EditConfig that show again. Probably something went wrong with my crooked hands... – Young wizzard Jun 25 '20 at 08:48
  • Delete them all and run it again by clicking on the play on the test file. This MainActivityTest.e and MainActivityTeste should not appear again – jeprubio Jun 25 '20 at 08:51
  • no, it appears again. thank you for help, I don't want to lost your time! – Young wizzard Jun 25 '20 at 08:53
  • It's really strange. You could search for `MainActivityTeste` in your files (`Find in Path` option of android studio), maybe this might give you a clue of something which is wrong – jeprubio Jun 25 '20 at 08:56