I ended up loading resources from the classloader. Since AS 1.3, the resources in src/test/resources are automatically copied in the classpath to be properly loaded with getResourceAsStream().
Thanks both of you for the answers. Le mar. 23 juin 2015 à 16:56, Michael Grafton <[email protected]> a écrit : > If your goal is to pull files off the file system in tests, the solution > I've used on my project is to simply put files on the classpath and call > Java's universal getResourceAsStream() method on Class. > > This is a unit testing technique that is as old as JUnit itself, but sadly > the Android build system does not support this out of the box (last I > checked). Android's unit testing support is a recent development so not > everything works as expected. > > The problem is that non-java files are not addressable on the classpath > from android unit tests. We got around this by adding a gradle task to copy > them over. It's pretty straightforward and there are examples floating > around the internet. > > Mike > > On Tue, Jun 23, 2015 at 2:50 AM, Michal Bendowski <[email protected]> > wrote: > >> Android-style resources (currently) don't "just work" in JVM unit >> testing. You can always create a mock Resources instance, specify the >> behavior of openRawResource(...) and pass it to your code under test. >> >> Michal >> >> >> On Monday, June 22, 2015 at 3:16:45 PM UTC+1, Pierre Degand wrote: >>> >>> Hello, >>> >>> Is it possible to have unit test's specific resources or assets ? >>> >>> In my main/res/raw, I have a ws.json file that is used by my production >>> code. >>> >>> I want in some unit test to load a modified version of this json. I >>> would like to have in test/res/raw another file called test.json and >>> then reference it in a test java file with R.raw.test. >>> >>> I don't want to override all the files in main/res/raw because I also >>> need them in the java test file. >>> >>> I tried to modify the source sets of my test but it doesnt work >>> >>> sourceSets { >>> test { >>> assets.srcDirs = ['src/test/res', 'src/main/res'] >>> } >>> } >>> >>> >>> Is this even possible with the current build system ? >>> >>> Thank you. >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "adt-dev" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> > >> For more options, visit https://groups.google.com/d/optout. >> > -- > You received this message because you are subscribed to a topic in the > Google Groups "adt-dev" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/adt-dev/xmMwsqCdT-Q/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- Pierre Degand -- You received this message because you are subscribed to the Google Groups "adt-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
