Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using assets in robolectric tests

In my robolectric tests I am trying to read a file. Since this file contains just the test data I have put it in assets of the tests.

The directory structure looks like this.
src/main/assets/prod_data_file
src/test/assets/test_data_file

However when I try to read the file by getShadowApplication().getAssets().open(MY_FILE_PATH) it gives me FileNotFoundException.

Does anybody know how to fix this?

like image 516
ankitrokdeonsns Avatar asked Jul 08 '14 12:07

ankitrokdeonsns


2 Answers

Since my tests are just reading these files and returning file data as a String I was able to do that via getClass().getClassLoader().getResourceAsStream(MY_FILE_PATH) and then read file. Dont really need to read to use getShadowApplication().getAssets().open(MY_FILE_PATH).

I just moved my test data files to src/test/resources and it worked. :)

like image 149
ankitrokdeonsns Avatar answered Oct 11 '22 08:10

ankitrokdeonsns


I was able to make it like this:

1) Copy (or create) AndroidManifest.xml in test folder

2) Add @Config(emulateSdk = 18, manifest = "src/test/AndroidManifest.xml") for this specific test

I was able to read file. So it means you could play with AndroidManifest class and custom runner (more inspiration here https://groups.google.com/forum/#!topic/robolectric/YvbdqRFcdnA)

I think it could be also possible to play with build.gradle:

android {
  sourceSets {
    androidTest.setRoot( 'src/test' )

    androidTest {
      assets.srcDir file( 'src/test/assets' )
    }
  }
}

but I'm not sure since plugin doesn't create any build variant but just tasks.

like image 36
Eugen Martynov Avatar answered Oct 11 '22 08:10

Eugen Martynov