Using Unit Testing Frameworks

Scala Test

ScalaTest 1.0 contains an annotation (org.scalatest.junit.JUnitRunner) that allows JUnit 4 to pick up tests. For example:

import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.Spec

@RunWith(classOf[JUnitRunner])
class SampleTest extends Spec with ShouldMatchers {
describe("Demo") {
it("should run") {
1 + 1 should be (2)
}
}
}

Unfortunately, this currently runs the tests as unrooted.

For those not using ScalaTest 1.0 you can use the runner provided at this page:  http://github.com/teigen/scalatest-junit4runner

Specs

Specification written with specs can be executed as JUnit 4.x tests (Alt+Shift+X T) or as Scala Application (Alt+Shift+X S), use the following "specs" template

(since 2010-11-30, the template is includes in the Scala-IDE, so type "specs<Ctrl+Space to complete>")

import org.junit.runner.RunWith
import org.specs._
import org.specs.matcher._
import org.specs.runner.{ JUnitSuiteRunner, JUnit }
//import org.scalacheck.Gen

@RunWith(classOf[JUnitSuiteRunner])
class ${name}SpecTest extends Specification with JUnit /*with ScalaCheck*/ {

"${name}" should {
"allow " in {
${cursor}
//0
}
"deny " in {
//0
}
}
}

object ${name}SpecMain {
def main(args: Array[String]) {
new ${name}SpecTest().main(args)
}
}

Please have a look at the specs User Guide for more information.

Note that, following specs terminology, 'systems under specification' and examples having nested examples are executed as TestSuites containing TestCases while "leaf" examples are executed as TestCases:

Result of running a Specs test in Eclipse

Using JUnit4

  1. Go to the properties dialog of your project
  2. Go to the Java Build Path->Libraries tab and add an external class folder. Choose the bin folder of your project.
  3. Go to the Order and Export tab and move your new class path entry to the top or at least above your source folder(s).
  4. Add JUnit4 to the build path of your project if you haven't already.

The Eclipse JUnit runner should find your tests now, but only if you explicitly specify a test class in the launch configuration. Suppose you have a project junit4-test with a test class:

package a

import org.junit._
import Assert._

class Tests {

@Test def sample() {
    assertEquals(42, 6*7)
  }
}

Your launch dialog should look like the following:

Junit4 configuration dialog in Eclipse