Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sample project demonstrating use of the Robolectric Android testing project

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.idea
aidls
code
libs
res
robolectric @ c28aea9
.classpath
.gitignore
.gitmodules
.project
AndroidManifest.xml
CONVERTING-EXISTING-APPS
MIT.LICENSE
README
RobolectricSample.iml
TestingAndroid.iml
build.properties
build.xml
default.properties

README

This is a sample project configured and setup to use Robolectric.  This project shows how to configure
your build file, how to layout your project, sample tests, etc.

There are caveats:

    - This project is setup to use IntelliJ (Pivotal's Java IDE of choice).  There currently is no support for Eclipse
    (Eclipse makes assumptions about the project layout).  If you're interested in getting this running on Eclipse,
    we need your help!

    - Currently the library has some requirements on using the Google API's versions of the SDK.

    - Sometimes IntelliJ's module settings dialog re-adds the "code/gen" directory as a source in the main Android
      module, causing an error to appear; just manually remove it to fix.

To install this sample app and run the tests:

    git clone git://github.com/pivotal/RobolectricSample.git
    cd RobolectricSample
    git submodule update --init
    android update project -p .

    ant clean test

To install the sample app on the emulator:
    ant debug
    adb -e install [-r] <your-project-name>-debug.apk

PROJECT LAYOUT

The project moves the source (and test) directory into a plain old Java module (named the code module) so IntelliJ
won't run the really slow dex step every time the project is built (every time you run tests).  The code module has a
compile dependency on android the sdk library, allowing you to write your classes as you normally would.

The Android resources generated classes (R.class) is generated into the code module, so your code can refer to it.

The main Android module depends on the code module, and performs the dex build step.

The optional aidls module contains any service aidls for your project. The main module has a dependency to it, but code
does not. Code generated from aidls is generated into the code module by the Android build task.


ANT

The project contains a build.xml configured for the layout of the project.  The file extends the standard Android
build.xml to support the layout of the project files.  The build.xml file defines a test target.

This sample project and build.xml file also has support for the aidls your application may define. Currently there are
no samples demonstrating this, but we have successfully used this setup on projects that define aidls.


WRITING TESTS

If you're reading this, you probably know that the Android SDK jar is basically unusable outside of the Emulator or a
device.  All sdk methods throw RuntimeExceptions("STUB!"). Robolectric de-fangs the sdk jar by rewriting the contents of
all the methods in the sdk at runtime. By default, the methods are rewritten to do nothing and return null.
You may provide a shadow class to handle specific method calls.  Robolectric provides many shadow objects, but it
is not complete, or necessarily completely realistic.  Most of what you'll need is there, but don't be surprised if you
have to write a shadow for the functionality you're testing.

Robolectric requires that tests classes are loaded with a custom test runner so that it has a chance to prepare for
calls into the sdk.  Tests can then be written as you normally would write them.  Helper methods exist to provide
access to the shadow objects backing the real Android objects (shadowOf()).

Robolectric handles inflation of views, string resource lookups, etc. Only some view attributes (id, visibility,
enabled, text, checked, and src) are currently parsed and applied to inflated views.


GETTING STARTED

For now we recommend that you start with this skeleton application and add your code to it. For buggy and wrong pointers
on how to convert your existing project, see CONVERTING-EXISTING-APPS.


CONTRIBUTING

We welcome contributions. Please fork and submit pull requests!
Something went wrong with that request. Please try again.