java - 使用库运行测试任务,但使用 Junit 的 gradle 中不存在包

标签 java android gradle dependencies junit4

我有一个名为 integrationRealTest 的 gradle 任务来运行特定的测试,项目结构如下:

/mainProject
    |- /ttlib
        |- build.gradle
        |- (Java Sources and files)
    |- /app
        |- build.gradle
        |- (Java Sources and files)
    |- settings.gradle
    |- build.gradle

应用程序的 build.gradle 如下:
apply plugin: 'com.android.application'
apply plugin: "jacoco"
repositories { jcenter() }
android {

    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.jackalkao.junit_cate_test"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
        }
    }
}
sourceSets {
    test {
        java.srcDir file('src/test/java')
    }
    integrationRealTest {
        java.srcDir file('src/test/java')
    }
}
configurations {
    integrationRealTestCompile.extendsFrom testCompile
}

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

task integrationRealTest(type: Test ) {
    println 'start integration test'
    testClassesDir = sourceSets.integrationRealTest.output.classesDir
    classpath = sourceSets.integrationRealTest.runtimeClasspath
    include '**/PerformanceTestSuite.class'
    jacoco {
        destinationFile = file("$buildDir/jacoco/integrationRealTest.exec")
        classDumpFile = file("$buildDir/classes/integrationRealTest")
    }
    dependencies {
        testCompile project(":ttlib")
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(":ttlib")
    testCompile 'junit:junit:4.12'
    testCompile project(':ttlib')
    integrationRealTestCompile project(':ttlib')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
    compile project(':ttlib')


}

ttlib build.gradle 如下所示:
apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {

        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {

        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
    compile 'com.android.support:design:25.1.0'
    testCompile 'junit:junit:4.12'
}

我也有 settings.gradle
include ':app', ':ttlib'

我在 ttlib(library) 包名称中有一个类“项目”是 com.example.jackalkao.tlib ,我想在应用程序中进行集成/单元测试,
我在命令行上运行了“$gradle integrationRealTest”,我得到了错误:
/home//Junit-cate-test/app/src/test/java/com/example/junit_cate_test/ClassA.java:4: error: package com.example.jackalkao.tlib does not exist
import com.example.jackalkao.tlib.Item;
                                 ^
/home//Junit-cate-test/app/src/test/java/com/example/junit_cate_test/ClassA.java:27: error: cannot find symbol
        Item item = new Item();
        ^
  symbol:   class Item
  location: class ClassA
/home/jackalkao/Junit-cate-test/app/src/test/java/com/example/junit_cate_test/ClassA.java:27: error: cannot find symbol
        Item item = new Item();
                        ^
  symbol:   class Item
  location: class ClassA
3 errors
:app:compileIntegrationRealTestJava FAILED

我确定它能够运行 Android Studio IDE 的这个测试使用按钮,而且我可以执行“$gradle test”定义任务来运行它。如何修复这个编译错误并运行集成/单元 bu 任务测试?

最佳答案

也许同样的问题? When using gradle 'java-library' the classes in the library cannot be referenced

对于提到问题的用户,请帮助使用更新的 Gradle 包装器:4.0-milestone-2 ,但不适合我...

对我来说有助于使用而不是新的 implementation (已弃用 compile )api在模块中:https://developer.android.com/studio/preview/features/new-android-plugin-migration.html#new_configurations

关于java - 使用库运行测试任务,但使用 Junit 的 gradle 中不存在包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130351/

相关文章:

java - 尝试将 Gradle 项目包含为依赖项,但 include() 不起作用

java.util.UUID 实习/回收

java - 如何检测 Spinner 是否在 Android 上单击打开

java - 警报管理器立即运行

android - Android Gradle构建工具DOC

Gradle脚本将maven工件从本地文件夹复制到另一个文件夹

java - fragment 不会显示多个项目

java - JAXB 解码器下 sun.misc.URLClassPath.getLoader 中的高锁争用

android - onCreate() 没有被调用

android - 从 Fragment 完成一个 Activity