我正在尝试使用 gradle android 构建系统运行 robolectric 测试。我已按照 此处给出的说明尝试使其工作,但是在使用gradle robolectric运行测试时,我严重陷入以下错误 -
/Users/Sreekanth/Documents/artoo/Code/Android/HelloWorld/src/test/java/com/example/helloworld/MyTest.java:8: package org.robolectric does not exist
import org.robolectric.RobolectricTestRunner;
我的项目具有以下结构 -
.
├── AndroidManifest.xml
├── assets
├── bin
├── build.gradle
├── gen
├── libs
├── res
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── helloworld
│ ├── MainActivity.java
└── test
└── java
└── com
└── example
└── helloworld
└── MyTest.java
这是我的build.gradle:
buildscript {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
}
}
apply plugin: 'android'
apply plugin: 'robolectric'
apply plugin: 'maven'
dependencies {
compile 'com.android.support:support-v4:13.0.+'
robolectricCompile 'org.robolectric:robolectric:2.0'
robolectricCompile group: 'junit', name: 'junit', version: '4.11'
}
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}
我的 MyTest.java 文件:
package com.example.helloworld;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class MyTest {
@Test
public void shouldHaveHappySmiles() throws Exception {
String appName = new MainActivity().getResources().getString(R.string.app_name);
assertThat(appName, equalTo("My Application"));
}
}
如果我使用新的 gradle 项目结构,我可以成功运行 robolectric 测试,但如果我使用如上所述的旧 eclipse 项目结构,则不能。