2010-01-26

Using Scala's Specs TDD library in Eclipse

In this post I will show you a basic example of the specs library for Scala and how to run it in Eclipse.

Specs is a Behavior-Driven-Design testing framework for Scala which looks a bit different than JUnit you might be used to. However, it can be integrated with JUnit so that you may run unit tests in Eclipse as if they were JUnit unit tests. Well, almost.
This example is based on specs-1.6.1 and I used Scala 2.7.7.

For this to work you must have the specs library in your classpath as well as JUnit 4 (I used v4.5).

You must also do a bit of configuration. You must do it once per project.

1. Go to the build path configuration of your project.
2. Select the Libraries tab and hit Add Class Folder.
3. Hit Create New Folder.
4. Name it _bin (any other name should do as long as this does not exist).
5. Select Advanced and Link to folder in the file system.
6. Select the bin folder (See the screenshot below for better explanation).
7. OK.
8. Make sure that this new folder (alias) is placed above the test folder in the Order and Export tab.
9. Clean the project, rebuild.


Link to folder in the file system

And now, let's look at a very simple test.

package me.m1key.clouds

import org.specs.runner.JUnit4
import org.specs.runner._
import org.specs._

class JCloudCollectionSpecTest extends JUnit4(CloudCollectionSpec)


object CloudCollectionSpec extends Specification {
 
  "A cloud collection" should {
    "allow for adding a value and then still contain it" in {
      val cloud = new Cloud("Michal", 30)
      val collection = new CloudCollection
      collection.add(cloud)
      collection.contains(cloud) mustEqual true
    }
    // More tests
  }
}

As you can see, this line:

class JCloudCollectionSpecTest extends JUnit4(CloudCollectionSpec)

does the trick. You can now right click the project in Eclipse, select Run As -> JUnit Test and off it goes!

Update 2010-03-31: As Eric has pointed out (see his comment below the article), there is a more convenient way of writing JUnit4 compatible tests declaration, namely:

package me.m1key.clouds

import org.specs.runner.JUnit4
import org.specs.runner._
import org.specs._


class CloudCollectionSpec extends SpecificationWithJUnit {
    //...
}

So it saves you an extra declaration and makes it more succinct (note that what was an object is now a class). Thanks Eric!

Notes:
One issue is that at this moment double clicking on test reports does not work.
I used the Eclipse Scala plugin version 2.7.7.

2 comments:

  1. Hi Michal,

    Thanks for giving a detailed description of this rather ugly eclipse hack!

    Just want to add that there's a shortcut to declare specifications as JUnit test case as this is a very frequent case:

    class mySpecTest extends SpecificationWithJUnit { ... }

    All of this (and more) is specified here: http://code.google.com/p/specs/wiki/RunningSpecs#Run_your_specification_with_JUnit4

    Eric.

    ReplyDelete
  2. Thanks Eric! I just checked it out and it works as you said. I'm going to update the article to mention this.
    Michal

    ReplyDelete