Skip to Content

Setting up an sbt-based project for Scala 2.11 with ScalaTest support

Posted on    3 mins read

This post describes how to set up a new Scala 2.11.7 project with ScalaTest 2.2.4 support using sbt 0.13.9 on Mac OS X 10.11 “El Capitan” with Java 1.8.

If set up as describes below, you can manage your project via sbt, and you will be able to run your test cases via sbt test. Also, after setting things up this way, the project can be opened and used in IntelliJ IDEA 14 as an sbt project.

First, install Homebrew as described on the Homebrew website. You can now install sbt via brew, by issuing brew install sbt.

Running sbt about shows that sbt 0.13.9 is now installed.

Next, we can start to create our sbt-based Scala project with ScalaTest. To do so, start by creating the folder structure, assuming our project is named Foo:

mkdir Foo
cd Foo
mkdir -p src/main/scala
mkdir -p src/test/scala

Now, we create file src/test/scala/BarTest.scala with the following content:

package BarTest

import org.scalatest.FunSuite
import Bar.Baz

class BarSuite extends FunSuite {
  test("Baz.addOne() adds one") {
    val baz = new Baz()
    assert(baz.addOne(5) == 6)
  }
}

Next, we create file src/main/scala/Bar.scala, which contains the unit under test:

package Bar

class Baz {
  def addOne(x: Int): Int = {
    x + 1
  }
}

The BarTest package has an external dependency on ScalaTest. We can use sbt in order to manage this dependency – to do so, create the following build.sbt file in the project’s root folder, Foo:

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"

We can now run sbt update, which will pull in ScalaTest.

We are now able to run our project’s test suite via sbt test – however, the result doesn’t look very promising:

[info] Set current project to foo (in build file:/Users/manuelkiessling/Foo/)
[info] Compiling 1 Scala source to /Users/manuelkiessling/Foo/target/scala-2.10/test-classes...
[error]
[error]      while compiling: /Users/manuelkiessling/Foo/src/test/scala/BarTest.scala
[error]         during phase: typer
[error]      library version: version 2.10.5
[error]     compiler version: version 2.10.5
[error]   reconstructed args: -classpath /Users/manuelkiessling/Foo/target/scala-2.10...
[error]
[error]   last tree to typer: Apply(method ==)
[error]               symbol: method == in class Int (flags:  )
[error]    symbol definition: def ==(x: Int): Boolean
[error]                  tpe: Boolean
[error]        symbol owners: method == -> class Int -> package scala
[error]       context owners: method testBazAddOne -> class BarSuite -> package BarTest

The problem? We pulled in ScalaTest version 2.2.4 for Scala version 2.11 – however, sbt uses Scala version 2.10.5 when trying to compile our code. Why? Because this is the Scala version that was used to build the sbt version we have installed via Homebrew. Quoting from the sbt docs: “If the Scala version is not specified, the version sbt was built against is used.”

“Not specified” refers to our project settings. By changing our build.sbt file as follows:

scalaVersion := "2.11.7"

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"

…the problem is resolved:

~/Foo$ > sbt test
[info] Set current project to foo (in build file:/Users/manuelkiessling/Foo/)
[info] Compiling 1 Scala source to /Users/manuelkiessling/Foo/target/scala-2.11/test-classes...
[info] BarSuite:
[info] - Baz.addOne() adds one
[info] Run completed in 215 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 5 s, completed Nov 2, 2015 2:21:52 PM

Using File -> Open… in IntelliJ 14, the project can be imported using the following wizard settings: