Scala Unit Testing with Eclipse

ScalaTest and Eclipse

Describes how to set up Eclipse for testing Scala using ScalaTest and JUnit4.

You loved Junit because it was well integrated into Eclipse.  This method allows you to write your test cases in Scala and be just as integrated, because it uses JUnit4 integration.

  • Add Junit to your project.  The quick way is to create a junit4 test case by right clicking on your class.  (Do it just to humor me, and eclipse, you can then delete it, once  it puts JUnit on the path).
  • Add scalatest-1.7.1.jar to your build path.
  • Create a class like this and you are done:  (More info is at
package stuff;

// These 3 are for JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
//import org.scalatest.FunSuite

// These 4 imports are for JUnitSuite
import org.scalatest.junit.JUnitSuite
import scala.collection.mutable.ListBuffer

class TwoSuite extends JUnitSuite { // also extend FunSuite

var sb: StringBuilder = _
var lb: ListBuffer[String] = _

@Before def initialize() {
sb = new StringBuilder("ScalaTest is ")
lb = new ListBuffer[String]

@Test def verifyEasy() {
assert(sb.toString === "ScalaTest is easy!")
lb += "sweet"

@Test def verifyFun() {
assert(sb.toString === "ScalaTest is fun!")

About SunKing2

Former web developer, guitar player, started Piano 2012 -yes still doing it! Recovery, sobriety, mental health advocate.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s