问题
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: unknown resource
Resources$NotFoundException: no such theme
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?
Thanks
回答1:
I had this same problem even with Robolectric v2.3-SNAPSHOT. Solved it by explicitly setting the manifest in the test file via the Config annotation, like
@Config(manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest extends AndroidTestCase {
...
}
回答2:
At the end I managed to solve it moving to Robolectric v2.3-SNAPSHOT.
You can find this SNAPSHOT here oss.sonatype.org/index.html#nexus-search;quick~org.robolectric
The same Jake Wharton plugin works well with this Robolectric v2.3-SNAPSHOT.
Perhaps this information might help someone else.
来源:https://stackoverflow.com/questions/21186622/gradle-robolectric-resources-notfoundexception-in-testing