Setup for non-maven

1,509 views
Skip to first unread message

Leif Ashley

unread,
Jun 15, 2012, 5:50:56 PM6/15/12
to robol...@googlegroups.com
I am wanting to use Robolectric in our development efforts and I'm a bit confused on a few points.

The examples all show maven. However we are using Idea to run/test via the IDE launch and scripted ant scripts for test builds. What's the best method to unit test an android application with Robolectric in this senario? Do I create a separate java project or an Android test project? Personally, I want to have it just run as a local test (i.e. not running on a USB/simulator device).

Chuck Greb

unread,
Jun 15, 2012, 6:13:12 PM6/15/12
to robol...@googlegroups.com
Using IntelliJ, you can either include Robolectric as a module (source) or a library (jar). Robolectric should be added as a dependency to your application module (above Android platform, scope TEST).

You should setup the tests as Java/JUnit (not an Android project test). Robolectric tests live inside the same module as your application. No emulator/device is required.

Also, check out the AndroidIntelliJStarter project for a good example of integration as a submodule.


Best,
Chuck

Leif Ashley

unread,
Jun 15, 2012, 6:19:32 PM6/15/12
to robol...@googlegroups.com
Cool - I'll look at the starter. I have it somewhat running with a quick test setup of my own, but I think I'm missing dependencies. I found this on their site:

"If you’re not using Maven to build your project, you’ll probably want to grab the latest robolectric-X.X.X-jar-with-dependencies.jar from Sonatype"

But for the life of me, I cannot figure out how to download the jar from Sonatype. lol

Chuck Greb

unread,
Jun 15, 2012, 6:24:21 PM6/15/12
to robol...@googlegroups.com
Highlight robolectric-1.2-SNAPSHOT-jar-with-dependencies.jar and then from the bottom-right panel choose the "Artifact Information" tab and then you will see a button to Download.

Justin

unread,
Jun 15, 2012, 6:27:35 PM6/15/12
to robol...@googlegroups.com
I actually created an end-to-end sample project that you can clone and run in Eclipse (thanks to Michael Portuesi's help):

Leif Ashley

unread,
Jun 15, 2012, 6:28:31 PM6/15/12
to robol...@googlegroups.com
Thanks again... that gets me further along, but now I get:

java.lang.RuntimeException: java.lang.ClassCastException: com.xtremelabs.robolectric.RobolectricTestRunner cannot be cast to com.xtremelabs.robolectric.internal.RobolectricTestRunnerInterface

It looks like the RobolectricTestRunner didn't implement the RobolectricTestRunnerInterface in this jar. Sound right to you?

Chuck Greb

unread,
Jun 15, 2012, 6:44:38 PM6/15/12
to robol...@googlegroups.com
I have never seen this error before. It looks like it's coming from this line but I'm not sure why. Are you using a custom test runner or class loader?

Leif Ashley

unread,
Jun 15, 2012, 6:50:15 PM6/15/12
to robol...@googlegroups.com
I just found the same line, and it is 234. Below is my whole class, with some lines removed to protect the innocent. But we never get to the setup method anyway because the constructor fails to complete. I agree though, the class and structure looks fine, and I'm not sure why this would throw the cast exception.

@RunWith(RobolectricTestRunner.class)
public class Test1 {


    @Before
    public void setUp() throws Exception {
       ...
    }

    @Test
    public void test() throws Exception {
       ...

Phil Goodwin

unread,
Oct 29, 2012, 9:54:33 AM10/29/12
to robol...@googlegroups.com
I'll bet it's a classloader problem. The RobolectricTestRunner uses a special classloader to instrument all of the Android jars and then loads all of the tests along with a copy of itself in that classloader. After that it delegates all of the calls made to itself to its copy within the classloader via the RobolectricTestRunnerInterface. It appears that something has gone wrong with that mechanism. Is there more than one copy of Robolectric floating around by any chance?

On Sun, Oct 28, 2012 at 9:56 AM, Thomas H <thomas.he...@googlemail.com> wrote:
Did you find a solution for this?
I have the same problem:

java.lang.ClassCastException: com.xtremelabs.robolectric.RobolectricTestRunner cannot be cast to com.xtremelabs.robolectric.internal.RobolectricTestRunnerInterface

Thomas H

unread,
Oct 30, 2012, 4:55:01 PM10/30/12
to robol...@googlegroups.com
I just saw that this thread is about non-maven projects. I am actually using maven.
Does this exception make more sense in a maven context? Perhaps some configuration error in the pom?
I will put together a minimal example to reproduce this problem and post it here later this week.

Thomas H

unread,
Nov 4, 2012, 3:26:45 PM11/4/12
to robol...@googlegroups.com
For me the cause of the problem is the new junit 4.11-beta-1. It seems they changed how Testrunners are invoked but I did not spot a related item in the junit change log [1]
On my machine this can be reproduced with the RobolectricSample [2].
Just update the junit dependency in the pom to 4.11-beta-1 and run mvn clean verify.
-> ClassCastExceptions in target/surefire-reports/*
If I switch back to junit 4.10 or junit-dep 4.10 everything works fine.

[1] https://github.com/KentBeck/junit/blob/master/doc/ReleaseNotes4.11.md
[2] https://github.com/pivotal/RobolectricSample

Thomas H

unread,
Nov 5, 2012, 5:41:28 PM11/5/12
to robol...@googlegroups.com
I think the 'Test execution order' feature that was introduced with 4.11-beta1 is the cause of the problem.
The feature is implemented using a new Annotation @FixMethodOrder(...).
In the constructor of 'org.junit.internal.MethodSorter' clazz.getAnnotation(FixMethodOrder.class) is called.
This triggers the classloader of the testclass (instantiiated by the RobolectricClassLoader) to load the @RunWith annotation and with it the RobolectricTestRunnerInterface.

All this is triggered from within RobolectricTestRunner constructor call to the super constructor of BlockJunit4ClassRunner.
At this point the loading of RobolectricTestRunnerInterface is not yet delegated to the parent classloader.
-> RobolectricTestRunnerInterface gets loaded with the RobolectricClassLoader instead of the classloader that loads the junit classes.

Maybe it is possible to add the delegate directives to the RobolectricClassLoader before it is passed to the super constructor?
This should fix the problem but I do not know if this breaks some other assumptions regarding the global state in the singletons and the 'RobolectricTestRunner.sInstrumented()' call.

David Saff

unread,
Nov 23, 2012, 2:49:33 PM11/23/12
to robol...@googlegroups.com
Hi, all.

I'm one of the maintainers of JUnit, and a Robolectric fan.  I haven't completely been able to grok what's going on here, but I'd love to be able to use JUnit 4.11 (now released) with the most recent Robolectric.  How can I help?

   David Saff

Magnus Knutas

unread,
Nov 23, 2012, 2:53:52 PM11/23/12
to robol...@googlegroups.com

Finally! Take a look at actionbarsherlock issues ;) Ill provide help howewer I can!

Thomas Keller

unread,
Sep 2, 2013, 6:15:43 AM9/2/13
to robol...@googlegroups.com
Has this bug ever been resolved for the old 1.2 development line and / or is it still present with Robolectric 2.x?
Reply all
Reply to author
Forward
0 new messages