How can I add unit tests to my Android projects in Android Studio (IntelliJ) easily?
To be more exact: I want to add a folder with test code (JUnit 4) and execute the unit tests from there using the regular installed JDK (not in an Android emulator). So far I added the folders 'test/java/' to my module and added 'test' as a source set and junit as a test dependency:
sourceSets {
instrumentTest.setRoot('src/test')
}
dependencies {
instrumentTestCompile 'junit:junit:4.+'
// ...
}
When I now select Run 'All Tests' on the test/java folder it gives me an UnsupportedOperationException.
What am I missing? How do you run your unit tests for Android projects?
Bonus points for a recommendation of a plugin that works like 'Infinitest' in Eclipse - where I can simply save a class and its unit test is automatically executed. :-)
PS: I do not want to use https://github.com/JakeWharton/gradle-android-test-plugin since that plugin seems to be deprecated already.
I have spent the past weeks (also) on that. Actually, on that on steroids, since I also had to put Robolectric into the pot.
Short answer: unless I confuse that error with something else, I think you are trying to run unit tests with the Android testrunner.
Long answer: I ended up with the deprecated plugin (after trying it, and then trying to put tests into androidTest folder) because it solved quite some issues on Jenkins for me. The downside is that it does not recognizes the folder itself as java or android code, so code completion works with everything except the content of the folder (for example it sees correctly the base project and all dependencies).
What I had to do run it in Android Studio was to create a JUnit (not Android) testrunner. It won't work out of the box probably, because the classpath order is messed up (although it works fine by just running tests in gradle).
Basically the manual steps I had to do are:
Edit the -classpath
argument doing the following:
Append the -classpath
you just created to the runner VM options after -ea
(I found this solution, or parts of it, in many places, a very good one is: http://kostyay.name/android-studio-robolectric-gradle-getting-work/ and it applies also to simple unit tests).
An alternative I did not try but I considered has been suggested this morning (see last post): https://groups.google.com/forum/#!topic/adt-dev/Y8-ppkWell0
I have a lot of scattered knowledge right now due to the many issues I discovered due to preview SW and the tons of info I had to go through to solve them, but if you have more specific issue on the topic, just let me know :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With