Unit test specific resources

29 views
Skip to first unread message

Pierre Degand

unread,
Jun 22, 2015, 10:16:45 AM6/22/15
to adt...@googlegroups.com
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.

Michal Bendowski

unread,
Jun 23, 2015, 5:50:36 AM6/23/15
to adt...@googlegroups.com
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

Michael Grafton

unread,
Jun 23, 2015, 10:56:35 AM6/23/15
to adt...@googlegroups.com
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

--
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 adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pierre Degand

unread,
Jun 23, 2015, 11:05:03 AM6/23/15
to adt...@googlegroups.com
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.

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 adt-dev+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Pierre Degand

Michael Grafton

unread,
Jun 23, 2015, 11:07:44 AM6/23/15
to adt...@googlegroups.com
Nice! Thanks for the good news...I can now take out the workaround in my gradle file.

Mike
Reply all
Reply to author
Forward
0 new messages