2015-12-15 71 views
0

我已經開始爲我的應用程序編寫單元測試。我正在使用Mockito來嘲笑對象。無法在android studio中導入mockito

This是我遵循的鏈接,在我的應用程序級gradle文件中包含mockito依賴項。 問題是我無法將mockito導入到我的測試類中。

這裏是我的應用程序build.gradle文件的參考。

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:1.3.0' 
     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+' 
    } 
} 

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.1" 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 23 
    } 

    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_6 
     targetCompatibility JavaVersion.VERSION_1_6 
    } 
    lintOptions { 
     disable 'HardcodedText','TextFields','OnClick' 
    } 
} 

repositories { 
    jcenter() 
    mavenCentral() 
    maven() { 
     name 'SonaType snapshot repository' 
     url 'https://oss.sonatype.org/content/repositories/snapshots' 
    } 
} 

ext { 
    robobindingVersion = 'latest.integration' 
    //robobindingVersion = '0.8.6-SNAPSHOT' 
} 

dependencies { 
    testCompile "org.mockito:mockito-core:1.+" 
    compile("org.robobinding:robobinding:$robobindingVersion:with-dependencies") { 
     exclude group: 'com.google.guava', module: 'guava' 
    } 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    testCompile('org.robolectric:robolectric:3.0-rc2') { 
     exclude group: 'commons-logging', module: 'commons-logging' 
     exclude group: 'org.apache.httpcomponents', module: 'httpclient' 
    } 
    apt "org.robobinding:codegen:$robobindingVersion" 
    compile 'junit:junit:4.12' 
} 

unable to import mockito

+0

嘗試用androidTestCompile http://stackoverflow.com更換testCompile/questions/31219624/android-studio-gradle-can not-find-mockito –

+0

當你明確引用'Mockito'類時,爲什麼要做靜態導入? – Henry

+0

你是否試過編寫像其他進口一樣的編譯代碼? – Umair

回答

0

手動導入的Mockito jar文件所做的事情對我來說。 要做到這一點,首先在您的應用程序目錄中創建一個名爲「libs」的目錄。請注意,該目錄應該與src/main和build目錄處於同一級別。 接下來,下載mockito jar文件並將其粘貼到libs目錄中。

包括到您的依賴於應用級的build.gradle文件:

dependencies { 
    compile files('libs/add-your-jar-file-name-here') 
} 

同步的gradle產出和應該做的工作。

請參考this答案以獲取更多快照答案。

1

我有同樣的問題,只是通過解決它:

compile "org.mockito:mockito-core:1.+"

希望它可以幫助=]

+0

雖然這可能適用於你的情況,甚至可能在這種情況下工作 - 你真的需要添加一些解釋,爲什麼這應該工作 - 或者是什麼導致這個問題,這是一個解決方案。 – ishmaelMakitla

相關問題