ScalaTest with Maven

With multiple languages working on the same JVM now, it is possible (and easy) to use different languages for your production and test code.

Why not to take this opportunity when it can be easier and faster to write test code. This can be also a good way to learn a new language. You can keep your production code using the language you are the most comfortable with and write your tests in Scala or Groovy.

This post describes how to configure Maven project to use Java as a production code language and Scala with a ScalaTest framework for unit tests.

Quick How-To:



  1. Add these dependencies to your pom.xml file […] <dependencies> […] <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.scalatest</groupId> <artifactId>scalatest_2.10</artifactId> <version>2.0</version> <scope>test</scope> </dependency> </dependencies> […]

  2. Add maven-scala-plugin to your pom.xml file. This will tell Maven to compile your Scala code. […] <build> […] <plugins> […] <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> <executions> <execution> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> […]

  3. Create new „scala” folder in src/test/scala

  4. If all of your tests will be written in Scala, you can remove „java” folder from src/test/java

  5. Create test scala class in the src/test/scala (keep the packages convention)

  6. Change test class to extend FlatSpec class (to use FlatSpec testing style) and add annotation @RunWith(classOf[JUnitRunner]).

With test method, class can look like this:</p> <pre class="lang:scala decode:true " >package ScalaTestProject

import org.scalatest.{Matchers, FlatSpec} import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner]) class AppTest extends FlatSpec with Matchers{

"An Application" should "work as expected" in {
    val expectedIsTrue = true
    expectedIsTrue shouldBe true


  1. Run maven build with mvn clean package
This should compile your Java production code, Scala test code and run your ScalaTest test. 

You can download ready project here:

More details:

In step 6 we are adding @RunWith(classOf[JUnitRunner]) annotation. This makes Maven to run ScalaTest tests using JUnit framework. It is possible to run ScalaTest tests using Scala Maven plugin.

This plugin allows you to run ScalaTest tests through Maven without requiring @RunWith(classOf[JUnitRunner]) annotations and access all functionality of the ScalaTest Runner, including parallel execution and multiple reporters.

More information about ScalaTest Maven plugin on the ScalaTest website.

There are also few possible styles of writing tests with ScalaTest. In the example above I use FlatSpec which is kind of default for ScalaTest 2.0+. One style you can be familiar with already (especially if you were doing Coursera Scala courses) is FunSuite. The test using FunSuite style looks like this:

More about ScalaTest styles on ScalaTest website again.


comments powered by Disqus