Re-release of gradle-android-test-plugin

7,616 views
Skip to first unread message

Mike Grafton

unread,
Mar 4, 2014, 7:10:42 PM3/4/14
to robol...@googlegroups.com
Hey folks,

Terry Chen and I (with help of many others here at Pivotal) have raised Jake Wharton's gradle plugin from the dead and re-released it on Maven Central under org.robolectric.gradle:gradle-android-test-plugin:0.9.1. We hope that with this plugin, the Gradle + IntelliJ users out there will have a solid TDD workflow at their disposal when writing Robolectric (or any JUnit) tests.

Key Improvements:
- Imports Gradle projects into Intellij / Android Studio cleanly. You can now right-click to run a single test or test suite.
- Works alongside Espresso tests (and in theory any Instrumentation test, though we haven't tried those).
- Runs Robolectric and Espresso tests separately from the command-line.
- Added a DSL to configure some JVM options and include/exclude Test file patterns.
- Add more test coverage and these tests now run on Travis CI.

We also released deckard-gradle, which illustrates the simplest possible usage of Robolectric + Espresso + Gradle using this and the official Android gradle plugin. The original deckard has now been renamed to deckard-maven.

This plugin is still in the early stages and we hope to improve it and the IDE experience greatly in the near future, but this should unblock quite a few projects immediately. If it doesn't, pull requests are always welcome :)

Mike & Terry




Diego Costantini

unread,
Mar 5, 2014, 3:04:03 AM3/5/14
to robol...@googlegroups.com
awesome news! :)

one thing wrong on the github page:
"Add test-only dependencies using the testCompile configuration:"
but then you show instrumentTestCompile.

I would fix it (or rephrase) because with the original plugin version, I actually needed both configurations to have code completion, and that also affected the instrument tests.

I hope the double declaration is not necessary anymore :)


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Diego Costantini

unread,
Mar 5, 2014, 3:21:46 AM3/5/14
to robol...@googlegroups.com
also:
classpath 'com.robolectric.gradle:gradle-android-test-plugin:0.9.+'

should be:
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'

anyway, the plugin doesn't work for me, I get:
9:19:27 AM Gradle 'myproject' project refresh failed:
Cannot add task ':my-app:testClasses' as a task with that name already exists.
I don't know if it's because I am using the android plugin 0.8.+, but I'd rather not move backwards.

sorry for not packing feedback together in one post.


On Wed, Mar 5, 2014 at 1:10 AM, Mike Grafton <mi...@pivotallabs.com> wrote:

John Rikard Nilsen

unread,
Mar 5, 2014, 8:01:04 AM3/5/14
to robol...@googlegroups.com
Awesome news, thanks a lot for keeping this thing alive. The part I am most interested in is executing single tests or all the tests of a test class, which so far was not possible (I think). I'm not able to do this with the new plugin either, at least not in android studio. I keep getting:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:185)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:168)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 253


Is there a way of doing this in command line?

Again, great news that you're putting an effort into this, I wish I could help but my groovy is not good enough Im afraid...

Mike Grafton

unread,
Mar 5, 2014, 12:27:02 PM3/5/14
to robol...@googlegroups.com
Thanks for the feedback Diego. I made the changes to the README.

About your problem...we'll take a look today. One thing on our list is to try the latest/greatest android plugin. Are you using product flavors by any chance? 

Mike

Mike Grafton

unread,
Mar 5, 2014, 12:29:11 PM3/5/14
to robol...@googlegroups.com
Thanks for trying it out, John! 

That problem is annoying and must be fixed by hand. See the documentation for deckard-gradle, which shows how to fix this in Studio - it involves hand-editing your .iml file.

Mike


Diego Costantini

unread,
Mar 5, 2014, 4:06:37 PM3/5/14
to robol...@googlegroups.com

Yes, tasty flavors :)

Sent from my Nexus 4

Diego Costantini

unread,
Mar 5, 2014, 5:36:34 PM3/5/14
to robol...@googlegroups.com

Question about junit and espresso coexisting:
by using instrumentTestCompile robolectric would be dexed also for espresso. When I tried it with robotium, I hit the "too many methods" exception. Isn't it a risk also for espresso?

And why did you choose to move away from testCompile? On the gradle's side Jake's version was almost perfect. Android studio was quite an issue though...

Sent from my Nexus 4

On Mar 5, 2014 6:27 PM, "Mike Grafton" <mi...@pivotallabs.com> wrote:

Mike Grafton

unread,
Mar 5, 2014, 5:46:34 PM3/5/14
to robol...@googlegroups.com
Hi Diego,

Yeah, in the current setup Robolectric is just another library that gets dexed and placed into the test APK. We didn't run into any problems because of this, but our investigation was only on some fairly small projects. We realize it is certainly not optimal that this happens.

We hitched a ride with instrumentTestCompile in order to force the IntelliJ/Studio import to work and recognize our dependencies. Eventually we think Robolectric/Junit tests will use a different source set than android tests. See our post earlier today on adt-dev for more context.

In terms of versioning, we've just found that we are not compatible with latest android plugin *at all* :( So we are working today to get compatible with the latest and greatest. However the failure you posted earlier appears to be related to flavors; once we get on gradle 1.11 and android plugin 0.9.0, we'll take a look at your problem. Thanks again for your patience.

Mike

Andrew Jack

unread,
Mar 7, 2014, 2:57:09 PM3/7/14
to robol...@googlegroups.com
Great work resurrecting this plugin.

I had an issue when a project is using more than one productFlavors or buildTypes. It displays the message: 
"Cannot add task ':AnApp:testClasses' as a task with that name already exists."

I have submitted a pull request which adds the name of the product flavor and build type to the 'testClasses' task which allows the project to build. 

It would be great if it can be merged, commented, or fixed so I can have my IDE unit tests back.

Thanks

Mike Grafton

unread,
Mar 7, 2014, 8:56:51 PM3/7/14
to robol...@googlegroups.com
This is awesome...thanks Andrew, and sorry for the omission. I will comment on the issue.

Mike


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Emmanuel Vinas

unread,
Mar 9, 2014, 10:25:54 PM3/9/14
to robol...@googlegroups.com
Any idea when that pull request (https://github.com/robolectric/gradle-android-test-plugin/pull/4) is going to be pushed ?

Mike Grafton

unread,
Mar 9, 2014, 11:54:46 PM3/9/14
to robol...@googlegroups.com
I'll take a look at it tomorrow.

Mike

Andrew Jack

unread,
Apr 1, 2014, 4:59:04 AM4/1/14
to robol...@googlegroups.com
The latest IntelliJ EAP adds support for android gradle plugin 0.9. Can the 0.9.4 be released please?

http://confluence.jetbrains.com/display/IDEADEV/IntelliJ+IDEA+13.1+135.666+Release+Notes

alpar...@prezi.com

unread,
Apr 1, 2014, 6:25:47 AM4/1/14
to robol...@googlegroups.com
Hi Mike,

when do you plan to release a new version which supports android gradle plugin 0.9 ?

Mike Grafton

unread,
Apr 1, 2014, 10:25:25 AM4/1/14
to robol...@googlegroups.com
We can release that immediately once we try it. 

Out of curiosity Andrew, how do we know that the EAP release supports that version of the plugin? I didn't see anything on those release notes about it.

Mike
Message has been deleted

Andrew Jack

unread,
Apr 1, 2014, 11:35:32 AM4/1/14
to robol...@googlegroups.com
From this issue: http://youtrack.jetbrains.com/issue/IDEA-121722

and when I tried it with my 0.8.+ project it asked me to update to 0.9.+

Mike Grafton

unread,
Apr 1, 2014, 11:14:39 PM4/1/14
to robol...@googlegroups.com
Unfortunately, even in IntelliJ 135.666,  I still can't get the single test in deckard-gradle to run when I use the HEAD of gradle-android-test-plugin against the android gradle plugin version >= 0.9.0.   The error is that it can't seem to find the compiled test class.

While it's possible to use our plugin with Android Studio, it is painful because of the constant re-writing of the IML file. Until we have a solution that works in IntelliJ, I'm not sure we should be releasing. 

I'm open to suggestions of course :)

Mike

Nico Küchler

unread,
Apr 3, 2014, 9:17:03 AM4/3/14
to robol...@googlegroups.com
Hey Mike,

instead of manipulating the iml file you can manipulate the classpath for test runs and include missing build dirs. So you don't must edit the iml file, but the classpath must manually be updated after each dependency change.

step by step inside android studio:
1. start an unit test, it will fail with stub! or missing classes
2. copy to an editor the output from terminal (you only need --classpath "...")
3. edit classpath add additional sources (expecting that you use debug variant)
- cut the path to junit4 and paste it as the first entry
- append at end: /<projectPath>/build/test-classes/debug
- append at end: /<projectPath>/build/resources/testDebug

To the release question:
I like the style having all tests inside the project, which will be tested, but with deckard-gradle the build need to much extra time. Because for unit tests also instrumentation test dependencies are compiled, and for instrumentations test the unit test dependencies. Dex don't like most of the unit test dependencies. Current for me I found it much easier to seperate robolectric test in an extra module, to avoid this mixed dependencies. You see an option to optimise this behaviour? So long i will stay at novodas plugin, here an ready to use example https://github.com/nenick/android-gradle-template

Diego Costantini

unread,
Apr 3, 2014, 9:20:56 AM4/3/14
to robol...@googlegroups.com
In last couple of AS releases i couldn't see the error anymore, so i don't know where to take the classpath from now.


Nico Küchler

unread,
Apr 3, 2014, 9:21:12 AM4/3/14
to robol...@googlegroups.com
Sorry I forgot to tell that the edited classpath must be used inside vm options at your unit test run configuration.

Diego Costantini

unread,
Apr 3, 2014, 9:24:47 AM4/3/14
to robol...@googlegroups.com
Yes, but when i run them with empty classpath, i cannot see anymore the string error.
This more or less since 0.5


On Thu, Apr 3, 2014 at 3:21 PM, Nico Küchler <kuchl...@gmail.com> wrote:
Sorry I forgot to tell that the edited classpath must be used inside vm options at your unit test run configuration.

Nico Küchler

unread,
Apr 3, 2014, 9:30:43 AM4/3/14
to robol...@googlegroups.com
The classpath should be still visible (I use 0.5.3):
- First what you already said, the vm options must be empty
- idea abridge the output at me to somthing like "/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java ..." but when i click on it, then the string will be expanded and i can see the classpath.

Diego Costantini

unread,
Apr 3, 2014, 9:49:41 AM4/3/14
to robol...@googlegroups.com
i'll check, thanks


Mike Grafton

unread,
Apr 7, 2014, 10:56:05 AM4/7/14
to robol...@googlegroups.com
Thanks for suggesting this workaround, guys.  This will probably be the best way to keep Android Studio users with a solution, as painful as it is.

For IDEA users, the "path" tab in module configuration seems to work. I've merged in the two pull requests necessary to roll forward to gradle android plugin >= 0.9.0 and the newest IDE changes.  

Hoping to release it this week.

Mike

Mathias Lidal

unread,
Apr 9, 2014, 7:06:00 AM4/9/14
to robol...@googlegroups.com
Just a quick comment. I started looking at deckard-gradle as was able to run the tests on intellij build 135.666 without fiddling with classpaths directly.

When I imported the project into intellij the test output path was set to deckard-gradle/build/test-classes/debug. Removing /debug from the path enables me to run the tests successfully. 

Regards 

Mathias Lidal

Glenn Jahnke

unread,
Aug 19, 2014, 7:45:47 AM8/19/14
to robol...@googlegroups.com
You rock Mike. Thanks for you and the other Pivots work! Some days I definitely wish I was still there working with you guys.

Glenn

Pierre Thomain

unread,
Sep 18, 2014, 8:56:43 AM9/18/14
to robol...@googlegroups.com
Hi guys,

I'm using the plugin (0.12.0) for one of my projects and I'm having some issues with the flavours. I've got 2 flavours defined as such:

 buildTypes {
        release {
            runProguard false
        }

        productFlavors {
            nap {
                applicationId "com.nap.android.api.country.nap"
            }

            mrp {
                applicationId "com.nap.android.api.country.mrp"
            }
        }
        //allows publication of flavours
        publishNonDefault true

        sourceSets {
            androidTest {
                setRoot('src/androidTest')
                manifest.srcFile 'AndroidManifest.xml'
            }
        }
    }

However i've getting the following issue: Error:Cannot add a SourceSet with name 'testDebug' as a SourceSet with that name already exists.

Commenting out one flavour fixes it. Any suggestion as to how to fix this would be greatly appreciated :)
Cheers,
Pierre

Jason Hite

unread,
Nov 2, 2014, 8:21:17 PM11/2/14
to robol...@googlegroups.com
I'm using Android Studio, and trying to get deckart to work, and I'm getting the Class not found: "com.example.activity.DeckardActivityRobolectricTest" error, after moving the jdk down to the bottom of the iml.  Any help would be greatly appreciated.  I tried pulling in the gradle-android-test-plugin:0.9.1... let me try using the plugin....android-test, and results in not found android test.

Eugene Beletskiy

unread,
Nov 14, 2014, 8:55:04 AM11/14/14
to robol...@googlegroups.com
Hi Jason, I've bumped into the same problem, did you manage to resolve the issue? Thank you!

Mike Trewartha

unread,
Nov 18, 2014, 4:07:14 PM11/18/14
to robol...@googlegroups.com
Also looking for a solution to the same issue!

Eugene Beletskiy

unread,
Nov 18, 2014, 4:08:36 PM11/18/14
to robol...@googlegroups.com
Mike, I've switched to Idea instead of Android Studio and it works.

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/xsOpEwtdTi4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.

Bademus l.

unread,
Nov 19, 2014, 5:06:44 PM11/19/14
to robol...@googlegroups.com
Please try the fix for AndroidStrudio https://gist.github.com/bademux/adcebd74753ec2d9398a
Tested with Roboelectric 2.4 and AndroidStrudio 0.8.14


On Monday, November 3, 2014 2:21:17 AM UTC+1, Jason Hite wrote:

Mike Trewartha

unread,
Nov 19, 2014, 5:11:14 PM11/19/14
to robol...@googlegroups.com
I’ll give that a shot tonight- appreciate it!

Mike Trewartha
Senior Software Engineer
GoSpotCheck

720.383.5838
mi...@gospotcheck.com
gospotcheck.com



Nico Küchler

unread,
Nov 20, 2014, 1:08:01 AM11/20/14
to robol...@googlegroups.com
There are more plugins out to support robolectric. My current favourite is https://github.com/JCAndKSolutions/android-unit-test because it have no manual effort. Just install the mentioned plugin for android studio and you are ready to start

Bademus l.

unread,
Nov 20, 2014, 3:17:56 AM11/20/14
to robol...@googlegroups.com
You need external plugin and
"tasks.findByName("assembleDebug").dependsOn("testDebugClasses")" is still needed

Andrey Rahimov

unread,
Dec 5, 2014, 3:54:27 AM12/5/14
to robol...@googlegroups.com
No success with AndroidStrudio 0.8.14 and  candidat4; Same test class not found; :(

Bademus l.

unread,
Dec 8, 2014, 5:14:23 PM12/8/14
to robol...@googlegroups.com
Sorry for putting the link here and there, but I feel responsibility for my prev. temp solution:
try this: https://groups.google.com/forum/#!topic/robolectric/Eg1drYGSMAs, bugreports are welcomed on github.

Gautam Korlam

unread,
Dec 17, 2014, 7:46:22 PM12/17/14
to robol...@googlegroups.com
I wrote a Gradle plugin to integrate Robolectric tests into Android Studio with minimal configuration.
If you are curious, please check out: https://github.com/kageiit/gradle-robojava-plugin
Reply all
Reply to author
Forward
0 new messages