Using ScalaTest

JUnit is a good starting point for unit testing Scala code. However, as you get more familiar with Scala, you’ll want to take advantage of Scala’s conciseness and idioms for unit testing as well. When you’re ready for that, you may want to graduate to using ScalaTest. ScalaTest is a testing framework written in Scala by Bill Venners, with contribution from many other developers. It provides concise syntax for assertions and functional style for testing both Scala and Java code.

ScalaTest doesn’t ship with Scala, so first you need to download it.[7] Once you download the JAR, set an environment variable, SCALA_TEST_JAR, to refer to the full path of that JAR.

Now that you’ve downloaded ScalaTest, let’s write a test, similar to the one we wrote using JUnit, but this time using ScalaTest.

UnitTesting/UsingScalaTest.scala
 
import​ org.scalatest._
 
import​ java.util.ArrayList
 
 
class​ UsingScalaTest ​extends​ FlatSpec ​with​ Matchers {
 
trait​ EmptyArrayList {
 
val​ list = ​new​ ArrayList[​String​]
 
}
 
 
"a list"​ should ​"be empty on create"​ in ​new​ EmptyArrayList {
 
list.size should be (0)
 
}
 
 
"a list"​ should ​"increase in size upon add"​ in ​new​ EmptyArrayList {
 
list.add(​"Milk"​)
 
list add ​"Sugar"
 
 
list.size should be (2)
 
}
 
}

We mixed ScalaTest’s FlatSpec and Matcher traits into the UsingScalaTest class. ScalaTest promotes RSpec-like syntax for writing tests with the should syntax. In the test, we first created a trait named EmptyArrayList. You can create multiple traits, one for each setup of test instances you’d like to use in tests. For instance, if you want to prepopulate the list with sample data, you can do that in yet another trait.

We mixed the EmptyArrayList trait into the two tests using the new EmptyArrayList. This readily makes the variable named list, that we created in the trait, available within the tests. The code in the tests is fairly self-descriptive.

Compile and run the test using the following commands:

 
scalac -d classes -classpath $SCALA_TEST_JAR UsingScalaTest.scala
 
scala -classpath $SCALA_TEST_JAR:classes org.scalatest.run UsingScalaTest

ScalaTest will mix in the traits as directed and exercise the test methods we wrote in the test to produce the following output:

 
Run starting. Expected test count is: 2
 
UsingScalaTest:
 
a list
 
- should be empty on create
 
a list
 
- should increase in size upon add
 
Run completed in 181 milliseconds.
 
Total number of tests run: 2
 
Suites: completed 1, aborted 0
 
Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
 
All tests passed.

ScalaTest is quite versatile. It comes with a number of built-in traits to facilitate mocking using multiple different tools. For example, if you use Mockito to create stubs, mocks, or spies, you’ll thoroughly enjoy the ease of use from Scala. Let’s take a look at that next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.22.163