ScalaTest 112: How to run ScalaTest unit tests in Eclipse

Problem: How do I use ScalaTest in Eclipse (or, How do I run my ScalaTest unit tests from Eclipse?)

Solution

I do a lot of work from the command line with Ant builds and similar things, but there are times I like to do things through Eclipse. Today I wanted to run my ScalaTest unit tests in Eclipse, and found this to be a straightforward task.

Besides Scala, Eclipse, and an Eclipse project, you'll need:

Put those two jar files in your lib directory, add then add them to your build path.

To make sure everything works, you'll want to run a simple test. You can use the following Scala class (an object, actually) as a class to test. Just name it StringUtils.scala in your Scala/Eclipse project:

com.valleyprogramming.scalatest

object StringUtils {
  
  def splitCamelCase(s: String): String = {
   return s.replaceAll(
      String.format("%s|%s|%s",
         "(?<=[A-Z])(?=[A-Z][a-z])",
         "(?<=[^A-Z])(?=[A-Z])",
         "(?<=[A-Za-z])(?=[^A-Za-z])"
      ),
      " "
   ).replaceAll("  ", " ")
  }

}

And then you can use this as a simple ScalaTest test class. For the purposes of this exercise, just name it StringUtilsTest.scala, and put it in the same folder as the first file:

com.valleyprogramming.scalatest

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter

/**
 * Requires ScalaTest and JUnit4.
 */
@RunWith(classOf[JUnitRunner])
class StringUtilsTest extends FunSuite with BeforeAndAfter {
  
  test("splitCamelCase works on FooBarBaz") {
    val s = StringUtils.splitCamelCase("FooBarBaz")
    assert(s.equals("Foo Bar Baz"))
  }

  test("splitCamelCase works on a blank string") {
    val s = StringUtils.splitCamelCase("")
    assert(s.equals(""))
  }

  test("splitCamelCase fails with a null") {
    val e = intercept[NullPointerException] {
      val s = StringUtils.splitCamelCase(null)
    }
    assert(e != null)
  }

}

Note: I'm not using the ScalaTest BeforeAndAfter functionality in this example, but I left that in the code because I typically do use it.

Once you have the jar files in your lib directory, and these two Scala tests classes, you can run the StringUtilsTest class in Eclipse. With this class active in your Eclipse editor, click Run, then Run As..., and you should see the option to run this class as a Junit Test. When you run these tests, the Eclipse JUnit view should open up, and the tests should run successfully, giving you a "green bar".

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote, and was published by O’Reilly in late 2013:

You can find the Scala Cookbook at these locations:

I hope this tutorial has been helpful. All the best, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.