Scala Testing with ScalaTest
Hello World Spec Test
Create a testing class in the src/test/scala directory, in a file named HelloWorldSpec.scala. Put this inside the file:
• This example is making use of FlatSpec and Matchers, which are part of the ScalaTest library.
•FlatSpec allows tests to be written in the Behavior-Driven Development (BDD) style. In this style, a sentence is used to describe the expected behavior of a given unit of code. The test confirms that the code adheres to that behavior. See the documentation for additional information.
Spec Test Cheatsheet
The tests below uses these values for the examples.
To verify the type for a given val:
Note that the brackets here are used to get type String.
To test equality:
Not Equal check
To test inequality:
To verify length and/or size:
To verify the type and message of an exception:
Include the ScalaTest Library with SBT
Using SBT to manage the library dependency, add this to build.sbt: