unit test

A collection of ScalaTest BDD examples using FunSpec

Table of Contents1 - Getting started2 - First steps3 - Adding Given/When/Then behavior (and ‘And’)4 - More on Given, When, Then, and And5 - Add more tests within ‘describe’6 - Testing Option/Some/None in a BDD test7 - Nesting describe blocks8 - Using ‘before’ and ‘after’9 - Mark tests as pending10 - Temporarily disabling tests11 - Testing expected exceptions12 - Assertions13 - Using matchers14 - Tagging your BDD tests15 - Other16 - More information

This page is very much a work in progress, but it currently shows a small collection of ScalaTest BDD examples. I’ll keep adding more BDD examples as time goes on. Also, in this article I assume that you are familiar/comfortable with Scala and SBT, and are at least slightly familiar with using ScalaTest.

How to run Scala project tests with SBT and ScalaTest

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.3, “How to run tests with SBT and ScalaTest.”

Problem

You want to set up an SBT project with ScalaTest, and run the tests with SBT.

Solution

Create a new SBT project directory structure as shown in Recipe 18.1, and then add the ScalaTest library dependency to your build.sbt file, as shown here:

Scala: How to use a String during testing instead of an InputStream

Imagine that you have a method that requires an InputStream, and you want to test that method. How can you test it?

In the Scala Cookbook I wrote how your method can accept a Source instead of a File to make it easier to test, so I won’t write any more about that right now.

But if you have a method that accepts an InputStream and you want to be able to test it with a String, here’s what you can do:

Software code too fluid to test right now

I love this comment from Martin Odersky, from the image shown, which comes from this link:

“Initially, things were too fluid to invest in tests. I simply did not know whether the design would hold up, had to fit a lot of pieces together first. But now is a good time to put these tests in place.”

So often people talk about “Test First Development” that I think they’re insane, or just regurgitating marketing-speak to sound good. There are times when you’re coding things like SARAH where you don’t know how things are going to shake out that “Test First” just doesn’t make sense. If you know what you’re getting into when you’re coding, Test First sounds good, but when you’re exploring, “Oh, this is how SARAH should work”, or, “I thought an FTP server worked like this but it works like that”, Test First doesn’t make sense. (Once you understand the domain, giddyup, test your heart out.)

How to use JUnit with Scala

Problem: You want to test your Scala code using JUnit.

Solution

Include the JUnit library in your project, and use it in the same way you’ve used it in Java projects, with a few minor changes.

Assuming you’re using SBT on your project, include JUnit into the project by adding this dependency line to your build.sbt file:

ScalaTest 111: How to use Mock objects with ScalaTest

Problem: You want to use a mock object framework in your ScalaTest tests, such as Mockito.

Solution

ScalaTest offers support for the following mock testing frameworks:

  • ScalaMock
  • EasyMock
  • JMock
  • Mockito

Because the support for each framework is similar, let’s take a look at using Mockito.

Before starting, imagine that you have a login web service for your application, and rather than call the real web service during your tests, you just want to mock one up.

ScalaTest 110: Temporarily disabling unit tests

Problem: When using ScalaTest, you want to temporarily disable one or more tests, presumably until you can get them working again.

Solution

When using BDD-style tests, change it method calls to ignore: