Monday, June 11, 2012

Using Scala and ScalaTest with Maven

In a typical scenario maven is used to build java projects.

In case you want to include Scala code in your project, you can do this by using the maven-scala-plugin.

The plugin has reasonable defaults, configuring a scala project with maven has become quite trivial.

All you have to do is to include the following code in your build section of your pom.xml:


This suffices if you place your Scala classes in src/main/scala and src/test/scala

The jvm arguments give the Scala compiler a little more space which may speed up the compilation process.

Running Unit Tests with Scala and Maven

I'm using JUnit and ScalaTest in my project.  As a matter of fact, it doesn't really matter which framework you choose for your tests, as long as you write tests. If maintenance of tests is more of a concern at a later phase of the project, it will pay of that you apply the same quality standards to your test code as you do for your production code. 

I've learned that tests need a certain amount of work and attention in order to give you the revenue they promise. In trivial cases it is ok to cut'n paste in order to give test cases some variation, but this gets easily out of control - so be sure you create helper methods and classes which encapsulate complicated setup strategies in order to achieve a code reuse. It's more or less always the same.

Coming back to ScalaTest, it is a very nice testing framework which allows you to pursue different styles of testing, whatever fits best for your taste or environment. Here is a taste of ScalaTest:

package nxs.editor.usecases

import org.junit.runner.RunWith
import org.scalatest.BeforeAndAfterAll
import org.scalatest.FlatSpec
import org.scalatest.Tag
import org.scalatest.junit.JUnitRunner

object ATag extends Tag("ATag")

class ExampleSpec extends FlatSpec with BeforeAndAfterAll {

  override def beforeAll() = {
    //  doReallyExpensiveSetup()

  override def afterAll() = {
    //    cleanup()

  "this" should "be really green" taggedAs (ATag) in { assert(true) }


ScalaTest is really very well documented on their site, have a look. 

You may have noticed I've using here an annotation org.junit.runner.RunWith - this is solely for the purpose to execute the Specs in Eclipse as tests. As soon as the IDE Support for ScalaTest is fully functional one could remove this annotation and have an equally comfortable integration. There is a video online where Bill Venners shows what can be expected for ScalaTest integration in Eclipse - I'm looking forward to it!

To integrate ScalaTest in your maven build you want to have something like this in your pom:
     <skipTests>true</skipTests><!-- disable surefire -->
   <!-- enable scalatest -->
   <!-- see -->
     <filereports>WDF TestSuite.txt</filereports>

you will also need the dependency for scalatest of course:


and a proper download site for the plugin:

   <id>oss sonatype</id>

But see for yourself here.

No comments:

Post a Comment