junit Android单元测试无法导入要测试的类

1aaf6o9v  于 2022-11-11  发布在  Android
关注(0)|答案(1)|浏览(262)

您好,我刚刚将我正在开发的一个应用迁移到了Androidx。但是在我迁移之后,androidTest模块无法引用测试的类。例如,我想测试com.work.appname.webservice包中的webservice模块,所以我需要将该类导入到我们的androidTest中。但是在我迁移之后,测试类无法找到要测试的类。它是com.work.appname.webservice。无法解析符号“webservice”Cannot Resolve Symbol Webservice
Gradle文件

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "kotlin-allopen"
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
apply plugin: 'org.jetbrains.kotlin.android.extensions'
androidExtensions {
    experimental = true
}

version = project.VERSION_NAME

buildscript {
    ext.kotlin_version = '1.3.0'
    repositories {
        mavenCentral()
        mavenLocal()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
}

repositories {
    mavenCentral()
    mavenLocal()
    maven {
        url "https://oss.sonatype.org/content/repositories/ksoap2-android-releases/"
    }
    maven {
        url "https://maven.google.com"
    }
    flatDir {
        dirs 'libs'
    }
    google()
}

android {
    compileSdkVersion 32
    buildToolsVersion "30.0.3"

    packagingOptions {
        exclude  'jsr305_annotations/Jsr305_annotations.gwt.xml'
        exclude  'build-data.properties'
        return void
    }

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 32
        versionCode Integer.parseInt(project.VERSION_CODE)
        versionName project.VERSION_NAME
        testInstrumentationRunner "com.xxxxx.testutil.MultiDexAndroidJUnitRunner"
        multiDexEnabled true
        applicationId "com.xxxxx.expenses"

        manifestPlaceholders = [
                'appAuthRedirectScheme': project.CALLBACK_SCHEMA
        ]
        buildConfigField "String", "CALLBACK_SCHEMA", '"' + project.CALLBACK_SCHEMA + '"'
    }
    buildTypes {
        release {
            zipAlignEnabled true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug{
            minifyEnabled false
            testCoverageEnabled = Boolean.parseBoolean(project.ENABLED_COVERAGE)
        }
    }

    flavorDimensions("productFlavors")

    productFlavors {
        expenses {
            applicationId "com.xxxxx.expenses"
        }
        expensesCitrix {
            applicationId "com.xxxxx.expenses.citrix"
        }
    }

    lintOptions {
        abortOnError false
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

configurations {
    compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
}

jacoco {
    toolVersion = "0.8.8"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

dependencies {
    implementation 'com.xxxxx:common:4.4.4'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation(name: 'google-maps-sdk-m4b', ext: 'aar')
    implementation 'commons-codec:commons-codec:1.15'
    implementation 'me.leolin:ShortcutBadger:1.1.22@aar'
    implementation 'commons-io:commons-io:2.11.0'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
    implementation 'junit:junit:4.13.2'

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'androidx.test.ext:junit:1.1.3'
    testImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.annotation:annotation:1.4.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test:core:1.5.0-alpha02'
    androidTestImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.10.0'

    androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0') {
        exclude group: 'com.google.code.findbugs'
    }
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.android.support', module: 'recyclerview-v7'
        exclude group: 'com.google.code.findbugs'
    }
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0', {
        exclude group: 'com.google.code.findbugs'
    }
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

allOpen {
    annotation("com.xxxxx.expenses.util.OpenClass")
}

我导入的时候好像只找到androidTest里面的类,有没有人知道解决的办法?谢谢

ou6hu8tu

ou6hu8tu1#

build.gradle 看,一切正常。我检查过了,导入的库有问题。如果您使用Android Studio从菜单Refactor〉Migrate to Android X迁移到Android X,您需要运行单元测试并查看构建错误。有时,从该菜单迁移到Android X的自动化功能无法正常工作。

例如:它应该是import androidx.fragment.app.Fragment而不是import androidx.core.app.Fragment。您可以转到错误行并突出显示错误,您将看到Import class按钮,只需单击

相关问题