gpt4 book ai didi

android - Robolectric With Gradle : .\res\values 不是目录

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:37 26 4
gpt4 key购买 nike

我正致力于使用 gradle 实现运行 robolectric 测试。

我目前正在执行所有测试,但是每当我尝试使用 Robolectric.buildActivity(ClassName.class).create().get();

实例化 Activity 时

编辑:使用 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 加载资源的方式?

最佳答案

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

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

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

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

关于android - Robolectric With Gradle : .\res\values 不是目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17986159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com