Why can't I import AndroidJUnit4 and ActivityTestRule into my unit test class?

后端 未结 8 499
一个人的身影
一个人的身影 2020-12-08 02:02

I\'m having trouble importing some of the Android UI testing framework classes - I just can\'t figure out what is going wrong!

This is my class:

@Run         


        
相关标签:
8条回答
  • 2020-12-08 02:03

    There are two different types of tests you can set up in Android

    Unit Tests

    • These run directly on the JVM and do not have access to the Android framework classes.
    • They are kept in the test/java package
    • Dependencies need to added in the build.gradle file with the command testCompile
    • You generally use Mockito, Robolectric & JUnit for these tests

    Instrumentation Tests

    • These run on an Android emulator and have full access to all the Android classes
    • They are kept in the androidTest/java package
    • Dependencies need to be added to build.gradle with androidTestCompile
    • You generally use Espresso and JUnit for these tests

    From what I can tell you are trying to write instrumentation tests with Espresso but have your test in the test/java package which is for unit tests. In that case you need to move your test class to the androidTest/java package.

    0 讨论(0)
  • 2020-12-08 02:07

    Add dependency.

    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test:runner:0.5'
    
    0 讨论(0)
  • 2020-12-08 02:11

    Adding:

    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    

    solves problem, but don't forget to sync the project with gradle files. Only then the changes will take effect.

    0 讨论(0)
  • 2020-12-08 02:23

    need this add dependencies

     testCompile 'com.android.support.test:rules:0.5'
     testCompile 'com.android.support.test:runner:0.5'
    
    0 讨论(0)
  • 2020-12-08 02:24

    For writing UI tests((tests that run on Android Device/Emulator)) in Android, make sure that

    1. The Test classes are in androidTest package rather that test package.

    2. Make sure you make the following dependencies in build.gradle

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    testImplementation 'junit:junit:4.13'
    

    For Unit Testing(tests that run on JVM) make sure

    1.Test classes are in test package

    2.Make sure you make the following dependencies in build.gradle

    testImplementation 'junit:junit:4.13'
    testImplementation 'org.mockito:mockito-core:2.23.0'
    
    0 讨论(0)
  • 2020-12-08 02:26

    Add these in the newer version:

    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    
    0 讨论(0)
提交回复
热议问题