Example Robolectric and Ant Eclipse Project

Until recently, I’ve been unable to configure a Robolectric project that can easily be shared across a team and integrated with Jenkins CI without the use of Maven. I mentioned this in a previous post, and have asked for assistance on Pivotal’s Robolectric Google Group a handful of times. There are several jar files that need to be referenced, and dependencies between the test and main Android projects and (without the use of Maven) this is difficult to configure and move around between teammates.

Thankfully, a brilliant Android engineer named Michael Portuesi came to my aid and helped me sort out the missing pieces, and I decided to create an example Robolectric project to share his wisdom and help other people get started. This project also includes an Ant script that you can easily plug into Jenkins or any other Ant-based CI process.

How does it work?

A Robolectric test project requires dependencies on:

  • android.jar
  • maps.jar
  • junit.jar
  • hamcrest.jar
  • robolectric-with-dependencies.jar
  • The Android project being tested

There are several ways you can set this up, but for simplicity I’ve pinned these jar files into the test project’s libs folder. Some people prefer linking the Robolectric source into their test projects, so they can modify the Robolectric code as necessary. You can also use environment variables in place of the Maps and Android jars, and add references to JUnit and Hamcrest to your test project’s build path via Eclipse.

After cloning this repository, add the android-project and android-project-test projects into an Eclipse workspace. The android-project is an Android project (obviously), and the android-project-test project is a plain old Java project. You should be able to right-click on the android-project-test project, and choose “Run As -> JUnit Test”, and the single test should run via Robolectric and JUnit. Alternatively, you should be able to right-click on the build.xml file and choose “Run As -> Ant Build”. This ant script can be used to run your Robolectric tests via a CI server like Jenkins.

Take note of SampleTestRunner.java. This class inherits from RobolectricTestRunner, and tells Robolectric how to resolve references to your Android project’s manifest and resources:
[java]
package com.justinschultz.tests.runner;

import java.io.File;

import org.junit.runners.model.InitializationError;

import com.xtremelabs.robolectric.RobolectricConfig;
import com.xtremelabs.robolectric.RobolectricTestRunner;

public class SampleTestRunner extends RobolectricTestRunner {
/**
* Call this constructor to specify the location of resources and AndroidManifest.xml.
*
* @param testClass
* @throws InitializationError
*/
public SampleTestRunner(@SuppressWarnings("rawtypes") Class testClass) throws InitializationError {
super(testClass, new RobolectricConfig(new File("../android-project/AndroidManifest.xml"), new File("../android-project/res")));
}
}
[/java]

When authoring tests, make sure your class names end with the word “Test”, as the ant script requires this. Also make sure that their @RunWith annotations use the SampleTestRunner.class, and NOT the RobolectricTestRunner.class:
[java]
@RunWith(SampleTestRunner.class)
public class SampleTest {
@Test
public void testBasicResourceValue() throws Exception {
String helloFromActivity = new MainActivity().getResources().getString(R.string.hello);
assertThat(helloFromActivity, equalTo("Hello World, MainActivity!"));
}
}
[/java]
Finally, make sure junit.jar is defined in your Ant script’s classpath BEFORE android.jar, otherwise Ant will throw an error:
[xml]
<path id="junit_classpath">
<pathelement path="${build.dir}"/>
<pathelement path="${android.project.classpath}"/>
<!– NOTE: junit.jar must come before android.jar! –>
<filelist refid="libs_jars"/>
<filelist refid="android_jars"/>
</path>
[/xml]
And that’s it. You should be able to clone the Android Eclipse Robolectric Example from GitHub and immediately run the sample test via Ant or JUnit in Eclipse.

Comments

12 responses to “Example Robolectric and Ant Eclipse Project”

  1. Ted Avatar

    This is HUGE. Many thanks. Saved me a ton of time.

  2. Billy Avatar
    Billy

    Great article, however I tried running these tests in eclipse, and keep getting the following warnings from the console:

    Warning: an error occurred while binding shadow class: ShadowNdefMessage
    Warning: an error occurred while binding shadow class: ShadowNdefRecord
    Warning: an error occurred while binding shadow class: ShadowNfcAdapter

    what could that be a result of?

    Thanks,

    Billy

  3. Justin Schultz Avatar

    Glad you enjoyed the article, Billy.

    The warnings about binding to shadow classes are because you’re using a version of the Android.jar file that doesn’t contain those classes.

    You’re probably pointing at an older Android.jar file with a newer Robolectric.jar. 🙂

    Hope that helps.

  4. Koen Avatar
    Koen

    Thx, this is exactly what I have been looking for for a couple of weeks now! 🙂

  5. James Avatar
    James

    I can run the base project no problem, but android-project-test won’t compile.

    Unbound classpath variable: ‘CLASSPATH’ in project ‘android-project-test’

  6. James Avatar
    James

    Nevermind. I don’t know why pointing to a nonexistent android SDK (android-8) would make that error occur, but when I updated the manifest in android-project to have a min SDK of 12 it ran.

    I’m having another issue witht he conversion of the reports after it passes in an ant build, but that’s probably user error too. 🙂

  7. Jason Avatar
    Jason

    Pivotal Labs have put effort into updating Robolectric… would it be possible to use these instructions with their new version? (see https://github.com/robolectric/robolectric).

    I haven’t had much success in trying. 🙁

  8. Jason Avatar
    Jason

    Specifically, there isn’t a RobolectricConfig class anymore, which undermines your solution. I’ve tried adding it back to the robolectric source, but I’m getting tons of compile errors upon trying to create my own jar.

    And obviously having both new and old jars won’t work.

  9. Jason Avatar
    Jason

    Got it working with the @Config(manifest="/path/to/projectbeingtested/AndroidManifest.xml") which tells Robolectric how to resolve references to your Android project’s manifest and resources. Thanks again.

  10. navigate to this website Avatar

    Hire now the best home addition contractors currently this week available in addition at the best price this week only!

  11. Klara Avatar

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now
    each time a comment is added I get several emails
    with the same comment. Is there any way you can remove people from that service?
    Thank you! https://Limarc.org/question/7-etapes-de-la-refection-de-toiture-3/

  12. affordable bathroom remodel near me Avatar

    Hire now a high quality general contractors missoula currently this week available and on sale this week only!

Leave a Reply

Your email address will not be published. Required fields are marked *