Java 使用Roboeletric和Gradle

Java 使用Roboeletric和Gradle,java,android,eclipse,gradle,robolectric,Java,Android,Eclipse,Gradle,Robolectric,我正在尝试设置Gradle以使用Roboelectric模拟框架运行Android测试 我有一个具有以下结构的Eclipse工作区: MyApp src gen test .... MyAppTest libs test (source folder linked to MyApp.test) .... 在Eclipse中手动配置构建路径,测试运行良好 我如何在MyAppTest中配置Gradle构建脚本,以便使用Roboelectri

我正在尝试设置Gradle以使用Roboelectric模拟框架运行Android测试

我有一个具有以下结构的Eclipse工作区:

MyApp
    src
    gen
    test
    ....

MyAppTest
    libs
    test (source folder linked to MyApp.test)
    ....
在Eclipse中手动配置构建路径,测试运行良好


我如何在MyAppTest中配置Gradle构建脚本,以便使用Roboelectric在MyApp项目中运行测试?

基于此,我能够使其正常工作

总之,尝试将以下内容添加到您的
build.gradle

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

dependencies {
    // Dependencies for your production code here.
    compile 'some.library'

    // localTest dependencies, including dependencies required by production code.
    testLocalCompile 'some.library'
    testLocalCompile 'junit:junit:4.11'
    testLocalCompile 'com.google.android:android:4.1.1.4'
    testLocalCompile 'org.robolectric:robolectric:2.2'
}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir

    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }

    classpath = sourceSets.testLocal.runtimeClasspath
}

check.dependsOn localTest
不要忘记将您的
*Test.java
更改为
@RunWith(RobolectricGradleTestRunner.class)

公共类RobolectricGradleTestRunner扩展了RobolectricTestRunner{
公共RobolectricGradeleteStrunner(类testClass)引发初始化错误{
超级(测试类);
}
@凌驾
受保护的AndroidManifest getAppManifest(配置){
String pwd=YourApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
字符串根=pwd+“../../../src/main/”;
返回新的AndroidManifest(
Fs.fileFromPath(root+“AndroidManifest.xml”),
Fs.fileFromPath(root+“res”),
Fs.fileFromPath(root+“资产”);
}
}

然后,您将能够通过
gradlecompiledbugjavalocaltest
运行测试。如果我没记错的话,这将需要一个较新版本的gradle(可能是1.8或1.9)

你应该让MyAppTest项目依赖于MyApp项目,例如这里-@EugenMartynov在MyAppTest中我已经配置了settings.gradle和includeFlat'MyApp',我也在build.gradle中设置了依赖关系(编译项目(':MyApp'))没有运气。当我在Gradle中启动测试任务时,没有运行任何测试。啊,那是因为android插件不支持单元测试。请在此处查看
build.gradle中的更改。注意,这仅适用于@如果您准备好使用
org.roblectric:roblectric:2.3-SNAPSHOT
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
    public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config) {
        String pwd = YourApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String root = pwd + "../../../src/main/";

        return new AndroidManifest(
                Fs.fileFromPath(root + "AndroidManifest.xml"),
                Fs.fileFromPath(root + "res"),
                Fs.fileFromPath(root + "assets"));
    }
}