单元测试使用终端的gradle进行CI

时间:2015-02-09 16:40:46

标签: unit-testing android-studio gradle

我一直在尝试使用Android Studio和Gradle进行单元测试,但现在我专注于使用./gradlew测试进行所有测试。问题是我从项目中的库中得到以下错误:

/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:20: error: cannot find symbol
    private MockContext context;
            ^
  symbol:   class MockContext
  location: class ITEventsAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITAssociationAPI.java:19: error: cannot find symbol
        RestClient rcAssociationsByEventID = new RestClient("");
        ^
  symbol:   class RestClient
  location: class ITAssociationAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITAssociationAPI.java:19: error: cannot find symbol
        RestClient rcAssociationsByEventID = new RestClient("");
                                                 ^
  symbol:   class RestClient
  location: class ITAssociationAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:30: error: cannot find symbol
        RestClient rcLeadsByEventID = new RestClient(base_url + "/rest/event/" + EVENT_ID);
        ^
  symbol:   class RestClient
  location: class ITEventsAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:30: error: cannot find symbol
        RestClient rcLeadsByEventID = new RestClient(base_url + "/rest/event/" + EVENT_ID);
                                          ^
  symbol:   class RestClient
  location: class ITEventsAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:38: error: cannot find symbol
        RestClient rcTagsCode = new RestClient(base_url + "/rest/event/tags");
        ^
  symbol:   class RestClient
  location: class ITEventsAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:38: error: cannot find symbol
        RestClient rcTagsCode = new RestClient(base_url + "/rest/event/tags");
                                    ^
  symbol:   class RestClient
  location: class ITEventsAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:46: error: cannot find symbol
        RestClient rcLeadEventsByTag = new RestClient(base_url + "/rest/eventtag/" + TAG + "/events");
        ^
  symbol:   class RestClient
  location: class ITEventsAPI
/Users/Radsen/Projects/Mobile/AllianceTech/androidstudio/ATDroidUtils/app/src/test/java/com/alliancetech/ileadsalpha/ITEventsAPI.java:46: error: cannot find symbol
        RestClient rcLeadEventsByTag = new RestClient(base_url + "/rest/eventtag/" + TAG + "/events");

这是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.alliancetech.ileadsalpha"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }

    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

sourceSets {
    unitTest {
        java.srcDir file('src/test')
        resources.srcDir file('src/test/resources')
    }
}

configurations{
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

dependencies {
    compile 'junit:junit:4.12'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.googlecode.json-simple:json-simple:1.1'
    compile 'org.roboguice:roboguice:2.0'
    compile project(':atdroidutilslib')

    //unitTestCompile 'junit:junit:4.12'
    //unitTestCompile 'com.googlecode.json-simple:json-simple:1.1'
    unitTestCompile "org.apache.httpcomponents:httpmime:4.2.3"
    unitTestCompile project(':atdroidutilslib')
}

task iLeadsIntegrationTests(type:Test, dependsOn: assemble) {
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn iLeadsIntegrationTests

1 个答案:

答案 0 :(得分:0)

在这个工作了将近2天之后,我决定重试我看过的一些帖子,这个帖子帮助我解决了我的问题。链接在这里: http://tools.android.com/tech-docs/unit-testing-support