December « 2013 « Matt’s Blog

Matt’s Blog Just another weblog


My Scala Notes

I've been working with Scala recently and just wanted to make this post about some tips that I'd like to remember.

Annotating Test Classes
When writing Scala tests, it's convenient to annotate them such that Eclipse is able to recognize how to execute the test from the "Run" menu. We can enable this by annotating the test class with a @RunWith statement. The IDE unfortunately doesn't seem to recognize that @RunWith and JUnitRunner are required imports when subsequently performing an "organize imports" operation. As a result, it's best to specify the fully qualified class name as shown on line two.

class MyScalaTest extends FunSuite {

Testing Exceptions in scalatest
Below is an example of verifying that code under test throws an exception.

test("throws an exception") {
    intercept[Exception] {
        throw new Exception("hi")

Working with Eclipse on Windows and Linux Console
I like to use eclipse, emacs, and the console during coding sessions. On my Windows machine I run Eclipse and access the source files over a network drive. This setup requires that my development life cycle be cross-platform compatible. To enable this workflow, it's convenient for sbt to generate the eclipse project file with all the required dependencies. This is done by adding,

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0")

to the plugins.sbt file and running sbt eclipse. Note, the version of the plugin will vary depending on your Scala version. It's best to visit the project page and try the latest version. Now, it's also helpful to add the following configuration to your build.sbt file,

// puts library dependencies in the lib_managed directory
retrieveManaged := true

This will download all dependencies to the project directory and the eclipse .project file will be able to reference them locally (as opposed to the ivy repo).

mvn dependency:tree in sbt?
There will come a time when you'll need to fight through dependency issues. One tool I frequently use is mvn dependency:tree. The equivalent of this command in sbt is enabled by including the following plugin,

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.4")

and the following to build.sbt,


The dependency tree can be generated by typing,

sbt dependency-tree

See this page for some more info.

Tagged as: No Comments