Hdr-ad-button-1

Scala IDE for Eclipse is an open source project powered by Assembla

Assembla offers secure, commercial-quality Hosted SVN and GIT Repositories, Ticketing & Bug Tracking, and Wikis.

Host your community projects for free or try our subscription plans free for 30 days.

Version 7, last updated by Anonymous at Jul 09 03:29 UTC

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.5 tests by either:

  • creating a class extending the org.specs.SpecificationWithJUnit class (preferred method)
    import org.specs._
    
    class mySpec extends SpecificationWithJUnit { ... }
    
  • or creating a class extending the org.specs.runner.JUnit4 class and taking your specification as a parameter
    import org.specs.runner.JUnit4
    
    class mySpecTest extends JUnit4(mySpec)
    object mySpec extends Specification { ... }
    

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