ScalaTest, Spec and Eclipse

By | November 21, 2009

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

2 thoughts on “ScalaTest, Spec and Eclipse

  1. Robert Post author

    Thanks, Tim! I’ve been very pleased with Maven. Also, Sonatype’s m2eclipse plug-in has been extremely helpful so far and was very easy to sort out, at least for everything I’ve needed to get started on building a Scala Lift app. I’ll definitely check out the Maven Handbook.

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.