使用库运行测试任务但是使用Junit在gradle中不存在包

时间:2017-05-23 09:04:26

标签: 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

app的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(库)包名中有一个类“Item”是com.example.jackalkao.tlib,我想在app中进行集成/单元测试, 我在命令行上运行“$ 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任务测试?

1 个答案:

答案 0 :(得分:0)

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

对于上述问题中的用户帮助使用较新的Gradle包装器:4.0-milestone-2,但不适合我......

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