4

我正在使用 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 构建

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 加载资源的方式?

4

3 回答 3

1

我能够通过将项目结构更改为类似来解决 ./res/value 问题

.Project Root
├── build.gradle
├── settings.gradle

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

└── test
    └── src
          └── java
于 2013-08-01T17:47:49.607 回答
0

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

于 2015-09-03T08:46:56.090 回答
0

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

于 2015-12-31T16:29:32.153 回答