我建议将 Robolectric 测试放在同一项目下的单独模块中。
要创建一个新模块,请转到 File -> New Module... -> Java Library。创建该模块后,导航到其build.gradle 文件并将其内容替换为以下行:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
repositories {
mavenLocal()
maven { url "$System.env.ANDROID_HOME/extras/android/m2repository" } // Fix 'com.android.support:*' package not found issue
mavenCentral()
}
evaluationDependsOn(":app")
apply plugin: 'java'
dependencies {
def androidModule = project(':app')
testCompile project(path: ':app', configuration: 'debugCompile')
def debugVariant = androidModule.android.applicationVariants.find({it.name == 'debug'})
testCompile debugVariant.javaCompile.classpath
testCompile debugVariant.javaCompile.outputs.files
testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath())
testCompile 'org.hamcrest:hamcrest-integration:1.1'
testCompile 'org.hamcrest:hamcrest-core:1.1'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
testCompile('org.robolectric:robolectric:2.4') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
testCompile "org.mockito:mockito-core:1.9.5"
testCompile 'org.assertj:assertj-core:1.6.1'
}
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Should.class"
include "**/*Test.class"
include "**/*Tests.class"
}
这使模块具有 Robolectric 测试的优点,应该作为一个很好的起点。
请注意,:app 应替换为您的应用程序模块的名称。
对于 Robolectric 测试,您还需要一个可能如下所示的 Robolectric 跑步者:
public class ApplicationTestRunner extends RobolectricTestRunner {
//Maximun SDK Robolectric will compile (issues with SDK > 18)
private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;
private static final String ANDROID_MANIFEST_PATH = "../app/src/main/AndroidManifest.xml";
private static final String ANDROID_MANIFEST_RES_PATH = "../app/src/main/res";
/**
* Call this constructor to specify the location of resources and AndroidManifest.xml.
*
* @throws org.junit.runners.model.InitializationError
*/
public ApplicationTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override protected AndroidManifest getAppManifest(Config config) {
return new AndroidManifest(Fs.fileFromPath(ANDROID_MANIFEST_PATH),
Fs.fileFromPath(ANDROID_MANIFEST_RES_PATH)) {
@Override
public int getTargetSdkVersion() {
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
}
};
}
}
然后只用这个运行器注释测试类,例如:
@RunWith(ApplicationTestRunner.class)
public class PriorityExecutorSchedulerTest {
....
}
可以使用 gradlew test 运行来自 Robolectric 模块的测试。
关于 Bamboo,AFAIK 有可能运行自定义脚本,所以我只需将 gradlew test 放入其中,看看效果如何。