Thursday, November 17, 2011 at 2:27PM Using Scala to Test Your Java Projects
In this technical blog coder-in-residence Viktor Nordling describes a sample project he has developed to showcase the ScalaTest test framework.
The Mission
I’ve been planning on starting to learn Scala by developing tests for my existing Java projects using the Scala language (following the advice of Java Posse’s Dick Wall). I originally found this article on StackOverflow but it didn’t really answer my questions. I then read an excellent blog article from ex-colleague Frederik Tyboni that descibed exactly what I wanted to do. Unfortunately for alot of readers his article is in Swedish. So here follows a rough translation of what he wrote — along with an example project that I have developed and posted on GitHub. My goal is to get you started with ScalaTest in minutes!
For the impatient, the example project is here
Integrating ScalaTest into Your Existing Maven Build
Assuming you are using Maven, all you need to do is first add the dependencies for Scala, JUnit and ScalaTest, like so:
org.scala-lang scala-library 2.9.1 test junit junit 4.8.1 test org.scalatest scalatest_2.9.1 1.6.1 test
Secondly, we add the maven-scala-plugin so that we can compile Scala code:
org.scala-tools maven-scala-plugin 2.15.0 testCompile -make:transitivenocp -dependencyfile ${project.build.directory}/.scala_dependencies
Now we tell the surefire-test-plugin to include **Spec files when executing tests:
org.apache.maven.plugins maven-surefire-plugin 2.6 false true **/*Spec.* **/*Test.*
Finally, use the build-helper-plugin to tell Maven to include source files under src/test/scala when compiling the tests.
org.codehaus.mojo build-helper-maven-plugin generate-test-sources generate-test-sources add-test-source src/test/scala
All we have to do now is to add a test. If you want to, you can use JUnit. In this way, you can use the more expressive syntax of Scala to write your tests, while still being in the familiar land of JUnit.
Or, you can take one step further and try a Scala testing framework. Here’s an example using ScalaTest, which is a test framework that integrates with JUnit and TestNG. It supports BDD if you’re into that and will make your tests more expressive than standard JUnit tests.
package com.cubeia
import org.junit.runner.RunWith
import org.scalatest.matchers.MustMatchers
import org.scalatest.FlatSpec
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class CalculatorSpec extends FlatSpec with MustMatchers {
"Calculator" must "multiply two values" in {
val calculator = new Calculator;
calculator.multiply(3, 5) must equal (15)
}
}
The Calculator class is written in Java, but tested from Scala. Again, you can get the source files for this if you clone my example project on GitHub.
That’s all! Happy Scala hacking!
—Viktor is a freelance software engineer who occasionally finds coding serenity at the Agile Digital office.
Agile Digital Editor | |
Post a Comment 
Reader Comments