Robolectric support for Specs2
Scala Java
Latest commit 428e2eb May 16, 2012 James Brechtel Update README.markdown
Failed to load latest commit information.
lib Setup application state before tests using before example; return spe… Apr 28, 2011
project use sbt 0.11 Nov 17, 2011
src Upgrade to specs2 1.3 release May 17, 2011
.gitignore Keep the res directory of the emptyAndroidProject so you can build af… Apr 29, 2011
build.sbt Use Scala 2.9.2 for Android dev, 2.9.1 suffers from android issue #22586 May 12, 2012



RoboSpecs lets you use Robolectric with Specs2.

Using Robolectric with Specs2 means that you mean write unit tests for your Android apps in Scala and run them in a JVM. Without Robolectric, Android unit tests must be run in an emulator. This is slow and painful enough to make TDD impossible with Android.

Robolectric provides a JUnit test runner which does some class loader magic at runtime to give your unit tests fake versions of Android framework classes. Robospecs is similar to Robolectric's JUnit test runner. Robospecs does not use JUnit.

Using RoboSpecs is really easy.

Download the latest jar

Get it here GitHub

Add an SBT dependency

val robospecs = "com.github.jbrechtel" %% "robospecs" % "0.2" % "test"
val robospecsSnapshots  = "snapshots" at ""
val snapshots = "snapshots" at ""
val releases  = "releases" at ""

Add a Maven dependency


Note: RoboSpecs is not thread safe

RoboSpecs' intialization of Robolectric (and possibly Robolectric itself) is not thread safe. By default SBT executes test suites in parallel and Specs2 runs specs within a suite in parallel. Add the following SBT options to run tests in serial.

parallelExecution in Test := false,
testOptions in Test += Tests.Argument("sequential"),

WARNING: When you use RoboSpecs do not attempt to place Android SDK code within the "should {} codeblock" or outside of it. Robolectric shadows are only attached within the "in {} codeblocks", so Android SDK code only works within them.

Example project

See a very simple, but working, example project here:

Here's the specs from that projects

Then you can mix it into your Specs2 specs like so:

package com.brechtel.toaster

import org.specs2.mutable._
import org.specs2.mock.Mockito
import org.specs2.matcher.{Expectable, Matcher}
import org.specs2.specification.BeforeExample
import com.github.jbrechtel.robospecs.RoboSpecs
import com.xtremelabs.robolectric.shadows._

class MainActivitySpecs extends RoboSpecs with Mockito {

  "onCreate" should {
    "set the content view" in {
      val activity = new MainActivity()
      activity.findViewById( must not beNull

  "showMessageButton" should {
    "be the Show Message button in the view" in {
      val activity = new MainActivity()
      activity.showMessageButton == activity.findViewById(

  "messageEditText" should {
    "be the EditText in the view" in {
      val activity = new MainActivity()
      activity.messageEditText == activity.findViewById(

  "clicking the showMessageButton" should {
    "show a toast popup with text from the message input field" in {
      val activity = new MainActivity()
      activity.messageEditText.setText("expected message")
      ShadowToast.getTextOfLatestToast must beEqualTo("expected message")