Typesafe Activator

Scala Testing

Scala Testing

typesafehub
Source
October 21, 2014
scala testing basics sample

A sample application demonstrating unit testing with various frameworks: ScalaTest, Specs2, ScalaCheck, JUnit, and TestNG.

How to get "Scala Testing" on your computer

There are several ways to get this template.

Option 1: Choose scala-testing in the Typesafe Activator UI.

Already have Typesafe Activator (get it here)? Launch the UI then search for scala-testing in the list of templates.

Option 2: Download the scala-testing project as a zip archive

If you haven't installed Activator, you can get the code by downloading the template bundle for scala-testing.

  1. Download the Template Bundle for "Scala Testing"
  2. Extract the downloaded zip file to your system
  3. The bundle includes a small bootstrap script that can start Activator. To start Typesafe Activator's UI:

    In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

    Or from a command line:

     C:\Users\typesafe\scala-testing> activator ui 
    This will start Typesafe Activator and open this template in your browser.

Option 3: Create a scala-testing project from the command line

If you have Typesafe Activator, use its command line mode to create a new project from this template. Type activator new PROJECTNAME scala-testing on the command line.

Option 4: View the template source

The creator of this template maintains it at https://github.com/typesafehub/activator-scala-testing#master.

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below, but it may work better if you view it inside Activator on your computer. Activator tutorials are often designed to be interactive.

Preview the tutorial

Scala Testing

A sample application demonstrating unit testing with various frameworks: ScalaTest, Specs2, ScalaCheck, JUnit, and TestNG.

This template is a companion to the Scala Testing video on the Typesafe YouTube channel. You can find the slides from the video in the "presentation" directory of the project.

For each framework, unit testing is demonstrated with tests written against scala's List class and a simple domain and service model located in the src/main/scala/models directory.

You can run the tests with sbt or your favorite IDE. Here are some helpful links:

ScalaTest

ScalaTest is a popular testing framework for Scala applications and supports a variety of testing styles by mixing in traits.

To use ScalaTest with sbt you need to add it as a managed dependency in build.sbt:


libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.6" % "test"
    
Scala IDE includes a ScalaTest plugin for running tests.

Sample test classes are located in the src/test/scala/scalatest directory and include the following:

  • ListFunSuite.scala: A set of tests against List using a style similar to classic JUnit.
  • ListWordSpec.scala: A set of tests against List using a popular Behavior Driven Development (BDD) style.
  • UserServiceSpec.scala: Demonstrates mocking dependencies using the MockitoSugar trait.

Specs2

Specs2 is a popular testing framework for Scala applications that uses a BDD style for test specifications.

To use Specs2 with sbt you need to add it as a managed dependency in build.sbt:


libraryDependencies += "org.specs2" %% "specs2" % "2.3.12" % "test"
    
You can run Specs2 tests in Scala IDE by adding a JUnitRunner annotation in your test classes:

import org.junit.runner.RunWith
import org.specs2.mutable.Specification
import org.specs2.runner.JUnitRunner

@RunWith(classOf[JUnitRunner])
class ListSpec extends Specification {
...
}
    

Sample test classes are located in the src/test/scala/specs2 directory and include the following:

  • ListSpec.scala: A set of tests against List.
  • UserServiceSpec.scala: Demonstrates mocking dependencies using the Mockito trait.

ScalaCheck

ScalaCheck is a library for automated property-based testing. Property checks are run multiple times with randomly generated inputs. You can use ScalaCheck directly or ScalaTest and Specs2 provide ScalaCheck integrations.

To use ScalaCheck with sbt you need to add it as a managed dependency in build.sbt:


libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.11.4" % "test"
    
A ScalaCheck Properties object can be run as a Scala application in Scala IDE.

Sample test classes:

  • src/test/scala/scalacheck/ListSpec.scala: A set of properties for List.
  • src/test/scala/scalatest/ListCheckersSpec.scala: ScalaTest using ScalaCheck integration.
  • src/test/scala/specs2/ListScalaCheckSpec.scala: Specs2 using ScalaCheck integration.

JUnit

JUnit is a widely used framework for unit testing Java. Because Java is interoperable with Scala, you can use JUnit for testing Scala as well.

To use JUnit with sbt you need to add the library and a test interface as managed dependencies in build.sbt:


libraryDependencies ++= Seq(
  "junit" % "junit" % "4.11" % "test",
  "com.novocode" % "junit-interface" % "0.9" % "test"
)
    
If you want to use add-on libraries such as Hamcrest or Mockito, you will need to add those dependencies as well:

libraryDependencies ++= Seq(
  "org.hamcrest" % "hamcrest-core" % "1.3" % "test",
  "org.mockito" % "mockito-core" % "1.9.5" % "test"
)
    
Scala IDE includes a JUnit plugin for running tests.

Sample test classes are located in the src/test/scala/junit directory and include the following:

  • ListTest.scala: A set of tests against List using asserts and Hamcrest matchers.
  • UserServiceTest.scala: Demonstrates mocking dependencies using Mockito.

TestNG

TestNG is a popular alternative to JUnit for testing Java. It can also be used for testing Scala.

To use TestNG with sbt you need to add the library as a managed dependency in build.sbt:


libraryDependencies += "org.testng" % "testng" % "6.8.8" % "test"
    
and add a plugin to project/plugins.sbt:

    addSbtPlugin("de.johoop" % "sbt-testng-plugin" % "3.0.0")
    
Scala IDE requires installation of a TestNG plugin for running tests.

Sample test classes are located in the src/test/scala/testng directory and include the following:

  • ListTest.scala: A set of tests against List.

comments powered by Disqus