尝试在android studio上运行测试但继续收到以下错误,似乎没有在gradle构建中找到testCompile导入的依赖项。当我使用compile而不是testCompile或androidTestCompile
时,它不会引起问题错误:(9,44)错误:包android.support.test.espresso不存在 错误:(9,1)错误:仅从类和接口导入静态 错误:(10,54)错误:包android.support.test.espresso.assertion不存在 错误:(10,1)错误:仅从类和接口导入静态 错误:(11,52)错误:包android.support.test.espresso.matcher不存在 错误:(11,1)错误:仅从类和接口导入静态 错误:(12,52)错误:包android.support.test.espresso.matcher不存在 错误:(12,1)错误:仅从类和接口导入静态 错误:任务':compileDebugJava'执行失败。 编译失败;有关详细信息,请参阅编译器错误输出。
的build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.0.2'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.application'
repositories {
maven { url "http://jzaccone.github.io/SlidingMenu-aar" }
mavenCentral()
jcenter()
maven {
url 'https://repo.commonsware.com.s3.amazonaws.com'
}
}
android {
ext {
compileSdkVersion = compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion = buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
minSdkVersion = Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion = Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
versionCode 18
versionName 'beta version 7'
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
applicationId 'com.closr.closr'
//testPackageName "androidTest.java.com.closr.closr"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// testFunctionalTest true
// testApplicationId "com.closr.closr.test"
//testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"
multiDexEnabled true
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src', 'src/main/java', 'src/androidTest']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// androidTest {
// setRoot('androidTest')
// java.srcDirs = ['androidTest/java']
// }
dexOptions {
incremental false
preDexLibraries = false
jumboMode = false
javaMaxHeapSize "2g"
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'LICENSE.txt'
}
lintOptions {
abortOnError false
}
}
signingConfigs {
debug {
storeFile file('C:/Users/Hobbs/.android/debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release {
storeFile file('C:/Users/Hobbs/.android/release.keystore')
storePassword 'closr123'
keyAlias 'Closr'
keyPassword 'closr123'
}
}
buildTypes {
debug {
minifyEnabled false
debuggable true
testCoverageEnabled = true
// proguardFile 'proguard.cfg'
// signingConfig signingConfigs.debug
}
release {
minifyEnabled true
proguardFile 'proguard.cfg'
signingConfig signingConfigs.release
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
project.tasks.withType(JavaCompile) { task ->
options.compilerArgs << "- AguiceAnnotationDatabasePackageName=com.closr.closr"
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
afterEvaluate {
// tasks.findByName("assembleDebug").dependsOn("testDebugClasses")
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = ['--multi-dex']
} else {
dx.additionalParameters += '--multi-dex'
}
}
}
}
apply plugin: 'android-unit-test'
androidUnitTest {
downloadDependenciesSources true
downloadDependenciesJavadoc true
downloadTestDependenciesSources true
downloadTestDependenciesJavadoc true
}
dependencies {
compile 'com.android.support:support-annotations:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
//compile 'com.android.support:appcompat-v7:21.0.3'
compile('com.google.android.gms:play-services:6.1.+') {
exclude module: 'support-v4'
}
compile 'com.android.support:multidex:1.0.0'
compile files('libs/aws-android-sdk-1.7.1.1.jar')
compile 'com.makeramen:roundedimageview:1.3.0'
compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+'
compile 'com.squareup.picasso:picasso:2.3.3'
compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'com.androidmapsextensions:android-maps-extensions:2.1.+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile 'com.jeremyfeinstein.slidingmenu:library:1.3@aar'
compile('com.commonsware.cwac:camera-v9:0.6.+') {
exclude module: 'support-v4'
}
compile 'com.squareup:otto:1.3.6'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okio:okio:1.2.0'
compile project(':libraries:facebook')
compile files('libs/Catalano.Core.jar')
compile files('libs/Catalano.Android.Image.jar')
compile files('libs/Catalano.Math.jar')
// robolectric
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'com.squareup:fest-android:1.0.+'
testCompile 'org.robolectric:robolectric:2.4'
// espresso
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
//compile ('com.android.support.test.espresso:espresso-contrib:2.0')
compile('org.roboguice:roboguice:3.+') {
//exclude group: 'javax.inject'
// exclude group: 'org.hamcrest'
// exclude group: 'junit'
}
provided 'org.roboguice:roboblender:3.+'
//compile 'com.google.code.findbugs:jsr305:3.0.
}
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.1.201405082137"
}
def coverageSourceDirs = [
'../src'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../build/jacoco/testDebug.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
测试课 包com.closr.closr;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import com.closr.closr.R;
import com.closr.closr.activities.MainActivity;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
@LargeTest
public class MainActivityInstrumentTest
extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mActivity;
@SuppressWarnings("deprecation")
public MainActivityInstrumentTest() {
super("com.closr.closr", MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
// Espresso will not launch our activity for us, we must launch it via getActivity().
getActivity();
}
public void testCheckText() {
onView(withId(R.id.txtDescription))
.check(matches(withText("Hello world!")));
}
}