【问题标题】:Class not found. Test class not found找不到类。未找到测试类
【发布时间】:2020-10-15 02:17:57
【问题描述】:

这是我的屏幕:

我应该设置哪些设置? 如何避免弃用类?

我想使用 espresso,之后尝试使用 UiAutomator,如何设置所需的必要设置?

我几乎输了一整天。

@RunWith(AndroidJUnit4::class)
@LargeTest
class MainActivityTest {

    @Rule
    @JvmField
    var activityTest = ActivityTestRule(MainActivity::class.java)

    fun c(){
        assertEquals(1,1)
    }
}

分级

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'
}

【问题讨论】:

  • 请将源代码添加为格式化文本而不是图像。
  • @TomStroemer ,全部完成

标签: android kotlin android-espresso


【解决方案1】:

您没有正确设置test runner

这一行:

"android.support.test.runner.AndroidJUnitRunner"

应该改为:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

因为您使用的是新的 androidx 库而不是旧的 support 库。

没有这个你会得到这个No tests were found 错误。不要忘记添加testInstrumentationRunner 关键字。

并将 junit 添加到 androidTestImplementation 也在 de gradle 文件中:

androidTestImplementation 'androidx.test.ext:junit:1.1.1'

【讨论】:

  • 我设置了“android.support.test.runner.AndroidJUnitRunner”,它抛出 java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present。同样在测试类中 @RunWith(AndroidJUnit4::class) 发光为已弃用
  • 将 junit4 添加到 androidTestImplementation 也在 gradle 文件中,而不仅仅是在 testImplementation 中。然后,一旦它工作,检查这个弃用错误:stackoverflow.com/questions/52776716/…
  • 感谢您的回复!我设置了 androidTestImplementation 'junit:junit:4.13'
  • 我试图开始测试,但它显示 java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present 这有点奇怪:(
  • 我设置了 androidTestImplementation 'androidx.test.ext:junit:1.1.1' 它不像弃用那样发光。但显示 java.lang.TypeNotPresentException: Type androidx.test.runner.AndroidJUnit4 not present。在你发给我的这个参考中,有这样的东西:将测试类从 AndroidJUnit4 更改为 AndroidJUnit4ClassRunner
猜你喜欢
  • 2016-10-17
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
  • 2018-08-16
  • 2012-03-29
相关资源
最近更新 更多