Learning Scala – day 2

In my first post on learning Scala I mentioned using ScalaTest. I’m going to walk through the steps (using IntelliJ community edition) to get up and running with ScalaTest as it’s a great way to try things out (ofourse you could alternatively use the Scala console).

  • Create a new project
  • Select Scala and SBT
  • Give the project a name, mine’s named Demo
  • If not already selected, select the JDK you want to support as well as the SBT and Scala version, or stick with the defaults if they’re already setup correctly

Upon finishing the new project wizard, you’ll get a standard layout of folders, as follows

src/main/scala
src/test/scala

In the root of the project you’ll get a build.sbt file. This is the Scala build system. If we open it, IntelliJ has supplied the bare minimum

name := "Demo"
version := "1.0"
scalaVersion := "2.12.1"

Before we do anything more let’s go into src/main/scala, right mouse click on the folder and select New Scala class. Give it the name Calculator, here’s the code

class Calculator {
  def add(a: Int, b: Int) = {
    a + b
  }

  def subtract(a: Int, b: Int ) = {
    a - b
  }
}

Now, go back to the build.sbt file and add the following

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"

When the build.sbt file was changed you’ll see it asking you to Refresh the Project, Enable auto-import or Ignore. I clicked Enable auto-import.

This will then import more Extendal Libraries (if you want to see what was imported, expand the External Libraries in the project view alt+1, if not already displayed)

Now, we could have done this in one step (but I wanted to demonstrate how the external libraries get added to), but now add the following

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

further external libraries will get imported if you refresh (or if auto-import is enabled, after a moment). These now include scalatest.

We’re going to let IntelliJ create our initial unit test fixture. To ensure everything is in place, carry out the following steps

  • Press the Project Structure button or File | Project Structure
  • In the Dependency tab or your project (i.e. mine’s demo) check the scalatest project has has been imported or add your preferred test package here
  • Press OK to close the dialog (or if no changes were made then Cancel is ofcourse fine)

Or if you’re happy all is ready then…

  • Load Calculator.scala into the editor
  • On the line of the class either right mouse click and select Goto | Test or the hot keys ctrl+shift+t
  • You should see a Create New Test popup
  • Select this to display a dialog
  • Now select the functions you want to create tests for, then press OK.

You’ll now be placed into the newly added CalculatorTest class and it’ll look like this

import org.scalatest.FunSuite

class CalculatorTest extends FunSuite {

  test("testAdd") {
  }

  test("testSubtract") {
  }
}

From here we can write our test code and further tests.

Like other test frameworks we now have access to assert functions, so for example we can have the testAdd look like this

test("testAdd") {
   val calc = new Calculator
    
    assert(calc.add(1, 2) == 3)
  }

org.scalatest contains a whole bunch of assert functions that we can use in our tests.

ScalaTest is not the only scala based test library

References

Working with Scala Tests
ScalaTest