Gradle + Robolectric: Where do I put the file org.robolectric.Config.properties?

。_饼干妹妹 提交于 2019-11-29 03:04:36

I finally found the right place to put it. First, the structure of my application is shown as below:

my application is under

app/src/main/

my test application is under

app/src/test/

You should name the property file as robolectric.properties and put it under

src/test/resources/ 

The content of robolectric.properties is

manifest: src/main/AndroidManifest.xml

Then, the test program can finally locate the property file!

The src/test/resources path as mentioned on the Robolectric blog seems to work for the org.robolectric.Config.properties file with the test plugin and Robolectric SNAPSHOTS. The RobolectricGradleTestRunner is no longer required.

Flo

I am using the robolectric gradle plugin and I faced (or am still facing ;) serious problems with junit testing in AS.

However, once thing that I just found out is the location of the org.robolectric.Config.properties. I too tried many places in the source project, but AS only started picking it up after I put it into build/test-classes.

You can have a look at my gradle build script at: Debug gradle-based unit tests with IntelliJ / AndroidStudio

Other steps necessary to get it unit tests running in AS:

  1. Reorder the <orderEntry type="jdk" ... /> to the end of the list in your <app>.iml file (this is to prevent the Stub!! exceptions, see: https://github.com/robolectric/deckard-gradle) - this step must be repeated, each time you sync with gradle.

  2. Add the compiled test files folder to your <app>.iml file. Add <output-test url="file://$MODULE_DIR$/build/test-classes" /> directly after <output url=... /> (see: Android Studio + Robolectric + Gradle Class Not Found Exception )

  3. Compile you app with gradle: gradlew app:clean app:testDebug or something like that. I noticed that AS does compile only the source files, but somehow does not compile the test classes, at least not anywhere into the /build folder. Gradle on the other hand properly compiles your test classes to /build/test-classes and with the configuration in step 2) AS will also pick them up, once compiled. Of course this step has to be repeated, each time you modify your test classes. (Note: I also found another solution from Eugen, see: https://stackoverflow.com/a/24140796/1406325 - i haven't tried that one yet, but it seems doing so does not require the extra step to compile with gradle)

  4. Put your org.robolectric.Config.properties into /build/test-classes (since with step 2) AS will pick it up now.

  5. If you also have references to other libraries, you might be in a need of a project.properties file (that is if you are facing Resources$NotFoundException problems when running your tests). Again, where to put this file? It goes into <app>/src/main (See: https://groups.google.com/forum/#!topic/robolectric/gjpwqRICT5U)

    android.library.reference.1=../../../library/src/main
    

Hope that helps!

Emil O

I had luck putting org.robolectric.Config.properties in src/test/res, but not src/test/resources.

Best way is to override getConfigProperties() in a subclass of RobolectricTestRunner by putting the property file in a folder and specifying the path as follows:

@Override
    protected Properties getConfigProperties() {
        FsFile fs = Fs.currentDirectory();
        InputStream resourceAsStream = null;
        try {
            File f = new File(fs.getPath() + <location-to-file-from-root-dir> + <file-name>);
            resourceAsStream = new FileInputStream(f);
        } catch (Exception e) {
            resourceAsStream = null;
        }
        if (resourceAsStream == null) return null;
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }

I see that you're following this: https://github.com/square/gradle-android-test-plugin

I think you should modify your @RunWith(RobolectricTestRunner.class) to the @RunWith(RobolectricGradleTestRunner.class)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!