Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RoboSpecs

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.1-SNAPSHOT" % "test"
val robospecsSnapshots  = "snapshots" at "http://jbrechtel.github.com/repo/snapshots"
val snapshots = "snapshots" at "http://scala-tools.org/repo-snapshots"
val releases  = "releases" at "http://scala-tools.org/repo-releases"

Add a Maven dependency

<dependency>
    <groupId>com.github.jbrechtel</groupId>
    <artifactId>robospecs</artifactId>
    <version>0.1-SNAPSHOT</version>
    <scope>test</scope>
</dependency>

Example project

See a very simple, but working, example project here: https://github.com/jbrechtel/robospecs_example

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.onCreate(null)
      activity.findViewById(R.id.message) must not beNull
    }
  }

  "showMessageButton" should {
    "be the Show Message button in the view" in {
      val activity = new MainActivity()
      activity.onCreate(null)
      activity.showMessageButton == activity.findViewById(R.id.show_message)
    }
  }

  "messageEditText" should {
    "be the EditText in the view" in {
      val activity = new MainActivity()
      activity.onCreate(null)
      activity.messageEditText == activity.findViewById(R.id.message)
    }
  }

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

About

Robolectric support for Specs2

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors