Questions And Answers

More Tutorials

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:

import org.scalatest.{FlatSpec, Matchers}
class HelloWorldSpec extends FlatSpec with Matchers {
 "Hello World" should "not be an empty String" in {
 val helloWorld = "Hello World"
 helloWorld should not be ("")

• 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.

val helloWorld = "Hello World"
val helloWorldCount = 1
val helloWorldList = List("Hello World", "Bonjour Le Monde")
def sayHello = throw new IllegalStateException("Hello World Exception")

Type check

To verify the type for a given val:

helloWorld shouldBe a [String]

Note that the brackets here are used to get type String.

Equal check

To test equality:

helloWorld shouldEqual "Hello World"
helloWorld should === ("Hello World")
helloWorldCount shouldEqual 1
helloWorldCount shouldBe 1
helloWorldList shouldEqual List("Hello World", "Bonjour Le Monde")
helloWorldList === List("Hello World", "Bonjour Le Monde")

Not Equal check

To test inequality:

helloWorld should not equal "Hello"
helloWorld !== "Hello"
helloWorldCount should not be 5
helloWorldList should not equal List("Hello World")
helloWorldList !== List("Hello World")
helloWorldList should not be empty

Length check

To verify length and/or size:

helloWorld should have length 11
helloWorldList should have size 2

Exceptions check

To verify the type and message of an exception:

val exception = the [java.lang.IllegalStateException] thrownBy {
exception.getClass shouldEqual classOf[java.lang.IllegalStateException]
exception.getMessage should include ("Hello World")

Include the ScalaTest Library with SBT

Using SBT to manage the library dependency, add this to build.sbt:

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"


In this page (written and validated by ) you learned about Scala Testing with ScalaTest . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Traits.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.