A few weeks ago I decided to learn Scala in order to develop the website for my latest side project. This led me down the road of trying to learn Google App Engine/Java, Lift, Maven, Git, ScalaTest, Spec and much more, so it’s been pretty slow going. Especially due to diversions into Cassandra, MongoDB, Tokyo Cabinet, Hadoop and HBase.

I’m a huge fan of JUnit for unit testing Java code, so I was happy when Bil Venners announced the release of ScalaTest 1.0 at a BASE meeting at Twitter. I really like being able to quickly run tests in Eclipse, but it wasn’t obvious to me how to do this with ScalaTest, especially when using the BDD styles of testing, like Spec. Creating test runners manually for each class was a deal killer. I finally found some info that mostly clarified things.

A simple example is helpful, so here you go:

import org.scalatest.Spec
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
 
@RunWith(classOf[JUnitRunner])
class MinimalSpec extends Spec {
   describe("My class") {
     it("should have testable behavior like...") {
       assert(true)
     }
   }
}

After creating the class, do one of the following:

  • Right click on file in tree and select Run As -> JUnit Test
  • Select file in tree and press Alt-Command-X + T on OS X or Alt-Shift-X + T on other OSs

After the test completes, the JUnit view should display something like:

Screenshot of JUnit view after running MinimalSpec with ScalaTest