Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Robolectric Resources NotFoundException in Testing

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

like image 827
Jose M Lechon Avatar asked Jan 17 '14 12:01

Jose M Lechon


1 Answers

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 {
...
}
like image 51
Nora Mullaney Avatar answered Sep 21 '22 22:09

Nora Mullaney