带有Gradle的Robolectric:。\ res \ values不是目录

时间:2013-08-01 05:54:23

标签: android unit-testing robolectric

我正在用gradle实现运行robolectric测试。

我正在执行所有测试但是每当我尝试使用Robolectric.buildActivity(ClassName.class).create().get();实例化活动时

编辑:使用HelloWorld activity = new HelloWorld();

生成相同的错误

我会收到以下错误:

 java.lang.RuntimeException: java.lang.RuntimeException: .\res\values is not a directory
        at org.robolectric.res.PackageResourceLoader.doInitialize(PackageResourceLoader.java:23)
        at org.robolectric.res.XResourceLoader.initialize(XResourceLoader.java:29)
        at org.robolectric.res.OverlayResourceLoader.doInitialize(OverlayResourceLoader.java:24)
        at org.robolectric.res.XResourceLoader.initialize(XResourceLoader.java:29)
        at org.robolectric.res.XResourceLoader.getValue(XResourceLoader.java:53)
        at org.robolectric.res.OverlayResourceLoader.getValue(OverlayResourceLoader.java:58)
        at org.robolectric.res.RoutingResourceLoader.getValue(RoutingResourceLoader.java:31)
        at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:263)
        at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:259)
        at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:85)
        at android.content.res.AssetManager.getResourceValue(AssetManager.java)
        at android.content.res.Resources.getValue(Resources.java:1009)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2098)
        at android.content.res.Resources.getLayout(Resources.java:852)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at org.robolectric.tester.android.view.RoboWindow.setContentView(RoboWindow.java:82)
        at org.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:272)
        at android.app.Activity.setContentView(Activity.java)
        at at.inaut.hw.HelloWorldActivity.onCreate(HelloWorldActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:5008)
        at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
        at org.robolectric.util.ActivityController$1.run(ActivityController.java:119)
        at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
        at org.robolectric.util.ActivityController.create(ActivityController.java:114)
        at org.robolectric.util.ActivityController.create(ActivityController.java:126)
        at at.inaut.hw.HelloWorldTest.testInstantiation(HelloWorldTest.java:23)

我的Gradle Build

import java.util.regex.Pattern

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

android {
       compileSdkVersion 17
       buildToolsVersion "17.0"

    sourceSets {

       main{
           manifest.srcFile 'AndroidManifest.xml'

       }
    }
}

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

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}


repositories {
    mavenCentral()
}

dependencies {
    compile project(':libs:actionbarsherlock')
}
sourceSets {
    unitTest {
        java.srcDir file('test')
        resources.srcDir file('test/resources')
    }
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/debug")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'org.robolectric:robolectric:2.1.1'
    unitTestCompile 'com.google.android:android:4.1.1.4'
    unitTestCompile files("${project(':libs:actionbarsherlock').buildDir}/classes/debug")
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
    }

    task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
    }

check.dependsOn unitTest

这是测试抛出错误的代码

导致错误的测试

activity =   Robolectric.buildActivity(HelloWorldActivity.class).create().get();
    testBtn = (Button)activity.findViewById(R.id.btnTest);
    assertNotNull(testBtn);
    assertThat(testBtn.getText().toString(), equalTo("testme"));
    assertNotNull(activity);

这可能与我的项目结构有关吗?或者它是robolectric加载资源的方式?

3 个答案:

答案 0 :(得分:1)

我能够通过将项目结构更改为类似

来解决./res/value问题
.Project Root
├── build.gradle
├── settings.gradle

├── build
└── src
  └── main
  |__res

└── test
    └── src
          └── java

答案 1 :(得分:0)

将complieSdkVersion 17更改为compileSdkVersion 21,将buildToolsVersion 17.0更改为targetSdkVersion 21.0.1

答案 2 :(得分:0)

Roboletric 3.0(不是Release Candidates,而是3.0版)解决了这个问题。该博客中详细介绍了此修复程序: https://philio.me/android-data-binding-with-robolectric-3/