问题描述
我已经知道这个问题已经被问过,但我解决不了这个问题。我能够编译和应用测试使用JUnit,Robolectric和摇篮。问题或错误来当我尝试测试活动或获取资源。每当我有同样的问题:
I've already knew that this issue has been asked before but I can't solve this problem.I am able to compile and apply test with jUnit, Robolectric and Gradle. The issue or error comes when I try to test an Activity or get a resource. Every time I've got the same issue:
android.content.res.Resources $ NotFoundException:未知的资源
资源$ NotFoundException:没有这样的主题
PageActivity startPageActivity = Robolectric.buildActivity(PageActivity.class).create().get();
Context context = Robolectric.getShadowApplication().getApplicationContext();
ShadowContext shadowContext = shadowOf(context);
assertNotNull( " No App name String " ,shadowContext.getString(R.string.app_name) );
这是该项目的结构:
And this is the project's structure:
.
├── build.gradle
├── settings.gradle
└── mainproject
├── build
│ ├── classes
│ │ └── debug
├── build.gradle
└── src
└── main
│ ├── AndroidManifest.xml
│ └── java
│ └── com...
│ └── resources
└── envDev
│ └── java
│ └── com...
│ └── resources
└── envQa
│ └── java
│ └── com...
│ └── resources
└── envLive
│ └── java
│ └── com...
│ └── resources
└── test
│ └── src
│ └── java
│ └── com...
│ └── rescources
└── testEnvDev
│ └── src
│ └── java
│ └── com...
│ └── rescources
└── testEnvQa
│ └── src
│ └── java
│ └── com...
│ └── rescources
└── testEnvLive
└── src
└── java
└── com...
└── rescources
这是在摇篮中定义的源设置:
This is the source set defined in gradle:
sourceSets {
main {
java.srcDir file('src/main/java')
manifest.srcFile file('src/main/AndroidManifest.xml')
res.srcDir file('src/main/resources')
}
envDev {
java.srcDir file('src/envDev/java')
manifest.srcFile 'src/envDev/AndroidManifest.xml'
res.srcDir file('src/envDev/resources')
}
envQa {
java.srcDir file('src/envQa/java')
manifest.srcFile 'src/envQa/AndroidManifest.xml'
res.srcDir file('src/envQa/resources')
}
envLive {
java.srcDir file('src/envLive/java')
manifest.srcFile 'src/envLive/AndroidManifest.xml'
res.srcDir file('src/envLive/resources')
}
instrumentTest {
java.srcDir file('src/test/java')
res.srcDir file('src/test/resources')
}
instrumentTestEnvDev {
java.srcDir file('src/testEnvDev/java')
res.srcDir file('src/testEnvDev/resources')
}
instrumentTestEnvQa {
java.srcDir file('src/testEnvQa/java')
res.srcDir file('src/testEnvQa/resources')
}
instrumentTestEnvLive {
java.srcDir file('src/testEnvLive/java')
res.srcDir file('src/testEnvLive/resources')
}
}
可能有人给一只手来面对这个问题?
Could someone give a hand to face this problem?
感谢
推荐答案
最后我设法解决其移动到Robolectric V2.3-快照。
At the end I managed to solve it moving to Robolectric v2.3-SNAPSHOT.
您可以在这里 oss.sonatype.org/找到此快照的index.html#承上启下搜索;快〜org.robolectric
同样的杰克·沃顿商学院的插件可以很好地处理这个Robolectric V2.3-快照。
The same Jake Wharton plugin works well with this Robolectric v2.3-SNAPSHOT.
或许,这信息可能帮助别人。
Perhaps this information might help someone else.
这篇关于摇篮Robolectric资源NotFoundException在测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!