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 http://www.scalatest.org/getting_started_with_junit_4_in_scala)
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
import _root_.org.junit.Test
import _root_.org.junit.Before

@RunWith(classOf[JUnitRunner])
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() {
sb.append("easy!")
assert(sb.toString === "ScalaTest is easy!")
assert(lb.isEmpty)
lb += "sweet"
}

@Test def verifyFun() {
sb.append("fun!")
assert(sb.toString === "ScalaTest is fun!")
assert(lb.isEmpty)
}
}
Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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