6. Testing with MUnit

So far we’ve checked Fully Booked by running it and reading what it printed. Reading output won’t tell us when a change breaks something that used to work. There’s also the flaw in freeAt that we’ve known about since chapter 4. We’ll catch it with a test and then fix it.

Scala’s standard library has no test framework, but the scala command can run several. I’ve chosen MUnit, which is small and will look familiar if you’ve used JUnit. ScalaTest is older and you’re likely to meet it in existing code, so there’s a section on it before the exercises.

6.1. A first test

MUnit is the first library we’ve needed that comes with neither Scala nor the JDK. Add a line to project.scala:

//> using test.dep org.scalameta::munit:1.3.6

If you’ve used Maven you’ll recognise the three parts as a group, an artifact and a version. A Scala library is published separately for each major version of Scala, with that version in the artifact’s name. The double colon asks for the build that matches our version of Scala, munit_3. A Java library is written with single colons throughout. Other people’s build files sometimes have a second double colon, before the version. That one only matters when Scala is being compiled for a platform other than the JVM. Writing test.dep in place of plain dep makes the library available to test code only.

Now create party.test.scala:

package fullybooked

class PartyTests extends munit.FunSuite:
  test("a party of four stays ninety minutes"):
    assertEquals(Party("Abara", 4).stayMinutes, 90)

  test("a party of five stays two hours"):
    assertEquals(Party("Okonkwo", 5).stayMinutes, 120)

  test("deposits start at six people"):
    assertEquals(Party("Okonkwo", 5).deposit, BigDecimal(0))
    assertEquals(Party("Haddad", 6).deposit, BigDecimal(60))

The .test.scala ending marks the file as test code. The scala command compiles such files separately. They can use everything in the project, but scala run ignores them and the main code can’t refer to them.

A suite of tests is a class that extends munit.FunSuite. Each call to test adds one test to it, with a name and a body. The body is the indented lines after the colon. Older code, and most of MUnit’s own documentation, puts it in braces, as in test("...") { ... }. Chapter 7 explains how a method can take a block of code as an argument.

The first argument of assertEquals is the value the code produced and the second is the value you expected. JUnit has them the other way round. If a failure message ever seems to be backwards, check the order.

The third test compares a deposit worked out as BigDecimal("10.00") * 6 with BigDecimal(60). The two are equal because Scala’s BigDecimal compares by value, where Java’s equals would also compare the scale and call 60.00 and 60 different.

Run the tests with scala test .:

$ scala test .
Test run fullybooked.PartyTests started
fullybooked.PartyTests: finished 0.01s
Test run fullybooked.PartyTests finished: 0 failed, 0 ignored, 3 total 0.01s

MUnit doesn’t say much when everything passes. The times will be different on your machine and on every run. In this chapter I’ve replaced them with round figures.

On Java 25 you’ll also get four lines of WARNING before any of that, about a method in sun.misc.Unsafe. MUnit 1.3.6 was compiled with Scala 3.3, and code from that compiler calls methods of an internal JDK class that Java now warns about and intends to remove. Other libraries compiled with Scala 3.3 or earlier print the same warning. The tests still run properly, so we’ll switch the warning off. Add a fourth line to project.scala, which passes an option to the JVM that runs the tests:

//> using test.javaOpt --sun-misc-unsafe-memory-access=allow

6.2. A test that fails

The flaw in freeAt is that it only asks whether a table is free at one moment. At eight o’clock it offers table 3, although the Moreaus have that table from quarter past. bestTable is built on freeAt and has the same problem. Before fixing it, we’ll write a test that shows it. Create availability.test.scala:

package fullybooked

import java.time.LocalTime

class AvailabilityTests extends munit.FunSuite:
  val eight = LocalTime.of(20, 0)
  val moreau = Booking(Party("Moreau", 3), Table(3, 4), LocalTime.of(20, 15), Status.Confirmed)

  test("the smallest table that fits is offered"):
    assertEquals(bestTable(Party("Silva", 2), eight, List(moreau)), Some(Table(1, 2)))

  test("a table that is booked during the stay is not offered"):
    assertEquals(bestTable(Party("Silva", 4), eight, List(moreau)), Some(Table(4, 4)))

The suite makes its own booking and leaves tonight alone. I keep test data in the test file. A test that depends on tonight.scala breaks when somebody edits that list for an unrelated reason, and anyone reading the test has to open a second file to see why it expects what it does.

Both tests ask for a table at eight with only the Moreau booking in the list. A party of two should get table 1. For a party of four, tables 3 and 4 are the smallest that fit, but the Moreaus will want table 3 fifteen minutes later, so the right answer is table 4. --test-only runs a single suite:

$ scala test . --test-only fullybooked.AvailabilityTests
Test run fullybooked.AvailabilityTests started
==> X fullybooked.AvailabilityTests.a table that is booked during the stay is not offered 0.01s munit.ComparisonFailException: .../fully-booked/availability.test.scala:13
12:  test("a table that is booked during the stay is not offered"):
13:    assertEquals(bestTable(Party("Silva", 4), eight, List(moreau)), Some(Table(4, 4)))
values are not the same
=> Obtained
Some(
  value = Table(
    number = 3,
    seats = 4
  )
)
=> Diff (- expected, + obtained)
   value = Table(
-    number = 4,
+    number = 3,
     seats = 4
    at munit.FunSuite.assertEquals(FunSuite.scala:12)
    at fullybooked.AvailabilityTests.$init$$$anonfun$2(availability.test.scala:13)
fullybooked.AvailabilityTests: finished 0.01s
Test run fullybooked.AvailabilityTests finished: 1 failed, 0 ignored, 2 total 0.01s

The second test got table 3 where there should have been table 4. MUnit shows that in the diff, where - marks what the test expected and + what it got. Above the diff are the lines of the test and the value the code produced. The line starting ==> X names the failed test and says where it is. I’ve shortened the path of the file, which MUnit prints in full. At the end the scala command prints Error: munit tests failed. and finishes with a non-zero exit status, which is what a build server checks.

The fix is to compare the party’s whole stay with each booking’s. Two stays overlap when each starts before the other ends. If you did exercise 7 in chapter 4 you’ve written freeFor already, and only need to move it into availability.scala. Otherwise add it there, above bestTable:

def freeFor(party: Party, time: LocalTime, bookings: List[Booking]): List[Table] =
  val end = time.plusMinutes(party.stayMinutes)
  val taken = bookings
    .filter(holdsTable)
    .filter(booking => time.isBefore(booking.leaveBy) && booking.time.isBefore(end))
    .map(_.table)
    .toSet
  Table.all.filterNot(taken.contains).filter(_.seats >= party.size)

Then replace bestTable with a version that uses it:

def bestTable(party: Party, time: LocalTime, bookings: List[Booking]): Option[Table] =
  freeFor(party, time, bookings).minByOption(_.seats)

Keep freeAt, because lastTable still prints with it. Run the tests again and both pass.

Add three more tests to the end of AvailabilityTests:

  test("a table is free again at the minute its party leaves"):
    val nakamura = Booking(Party("Nakamura", 2), Table(1, 2), LocalTime.of(18, 30), Status.Seated)
    assertEquals(nakamura.leaveBy, eight)
    assert(freeFor(Party("Silva", 2), eight, List(nakamura)).contains(Table(1, 2)))

  test("a cancelled booking does not hold its table"):
    val cancelled = moreau.copy(status = Status.Cancelled)
    assertEquals(bestTable(Party("Silva", 4), eight, List(cancelled)), Some(Table(3, 4)))

  test("nobody gets a table when the party is too big"):
    assertEquals(bestTable(Party("Zielinski", 9), eight, List()), None)

The first of them opens with an assertEquals that checks the Nakamuras really are due to leave at eight. If the length of a stay ever changes, that line fails first and says why. The test’s second assertion is a plain assert, which takes any Boolean. Use assertEquals when you’re comparing two values, because a failed assert can only report that something was false.

To see every test listed, pass -v to MUnit. Anything after -- goes to the test framework and not to the scala command:

$ scala test . --test-only fullybooked.AvailabilityTests -- -v
Test run fullybooked.AvailabilityTests started
fullybooked.AvailabilityTests:
fullybooked.AvailabilityTests.the smallest table that fits is offered started
  + the smallest table that fits is offered 0.01s
fullybooked.AvailabilityTests.a table that is booked during the stay is not offered started
  + a table that is booked during the stay is not offered 0.01s
fullybooked.AvailabilityTests.a table is free again at the minute its party leaves started
  + a table is free again at the minute its party leaves 0.01s
fullybooked.AvailabilityTests.a cancelled booking does not hold its table started
  + a cancelled booking does not hold its table 0.01s
fullybooked.AvailabilityTests.nobody gets a table when the party is too big started
  + nobody gets a table when the party is too big 0.01s
fullybooked.AvailabilityTests: finished 0.01s
Test run fullybooked.AvailabilityTests finished: 0 failed, 0 ignored, 5 total 0.01s

6.3. A new store for each test

The store is the one part of Fully Booked that changes as the program runs. That makes it awkward to test, because a booking added by one test mustn’t still be there when the next one starts. MUnit can make a new value for each test with what it calls a fixture. Create store.test.scala:

package fullybooked

import java.time.LocalTime

class StoreTests extends munit.FunSuite:
  val eight = LocalTime.of(20, 0)

  val emptyStore = FunFixture[BookingStore](
    setup = _ => InMemoryBookingStore(),
    teardown = _ => ()
  )

  emptyStore.test("a new store has no bookings"): store =>
    assertEquals(store.all, List())

  emptyStore.test("at finds the bookings for a time"): store =>
    store.add(Booking(Party("Lindqvist", 7), Table(6, 8), eight))
    store.add(Booking(Party("Fitzgerald", 2), Table(2, 2), LocalTime.of(21, 15)))
    assertEquals(store.at(eight).map(_.party.name), List("Lindqvist"))

  emptyStore.test("reserve saves a confirmed booking"): store =>
    val made = reserve(store, Party("Silva", 2), eight)
    assertEquals(made.map(_.table), Some(Table(1, 2)))
    assertEquals(store.all.map(_.status), List(Status.Confirmed))

A FunFixture is built from two lambdas, and the type in square brackets is the type of value it provides. Before each test MUnit runs setup to make that value, and afterwards it runs teardown to clear up. It hands setup some details of the test and hands teardown the value that setup made. We need neither here, which is why each lambda takes its argument as _. A test declared with emptyStore.test gets the new store as a parameter: its body starts store => after the colon and carries on beneath.

Our teardown does nothing, a sign that we could have managed without a fixture: val store = InMemoryBookingStore() as the first line of each test would have done. I use a fixture when there’s something to clear up afterwards, such as a temporary file or a database connection. If you’re used to @BeforeEach in JUnit, MUnit also has beforeEach and afterEach methods you can override.

Run all the tests. Your suites may come out in a different order from mine:

$ scala test .
Test run fullybooked.PartyTests started
fullybooked.PartyTests: finished 0.01s
Test run fullybooked.PartyTests finished: 0 failed, 0 ignored, 3 total 0.01s
Test run fullybooked.StoreTests started
fullybooked.StoreTests: finished 0.3s
Test run fullybooked.StoreTests finished: 0 failed, 0 ignored, 3 total 0.3s
Test run fullybooked.AvailabilityTests started
fullybooked.AvailabilityTests: finished 0.01s
Test run fullybooked.AvailabilityTests finished: 0 failed, 0 ignored, 5 total 0.01s

The StoreTests suite took three tenths of a second, nearly all of it the Thread.sleep(300) inside reserve. One sleeping test is tolerable, but a few hundred would make a suite that takes minutes, and a suite that slow tends not to get run before every commit. Chapter 21 shows how to test code that waits without the test taking that long.

6.4. Running some of the tests

The --test-only option takes a pattern as well as a full name, so --test-only 'fullybooked.Avail*' works. To choose tests by their names, give MUnit a pattern after the --:

$ scala test . --test-only fullybooked.PartyTests -- '*deposit*' -v
Test run fullybooked.PartyTests started
fullybooked.PartyTests:
fullybooked.PartyTests.deposits start at six people started
  + deposits start at six people 0.01s
fullybooked.PartyTests: finished 0.01s
Test run fullybooked.PartyTests finished: 0 failed, 0 ignored, 1 total 0.01s

While you’re working on one test you can also mark it in the code. A test declared as test("...".only) is the only one in its suite that runs, and .ignore in the same place skips a test. Take either out again before you commit. Chapter 11 explains how a String comes to have methods called only and ignore.

With --watch on the end, scala test . re-runs the tests every time you save. Metals and IntelliJ both show a run button beside each test.

6.5. What’s worth testing

Some tests you might write in Java aren’t needed here. No method in Fully Booked accepts or returns null. A match on Status that forgets a case is reported by the compiler, and so is an Option[Table] passed where a Table is wanted. That last check holds inside a test as well: assertEquals(bestTable(...), Table(1, 2)) doesn’t compile, because MUnit refuses to compare an Option[Table] with a Table. There’s also no point testing equals, toString or copy on a case class, which the compiler generates.

Nothing in the types says that a party of four stays ninety minutes and a party of five stays two hours, or that a table is free again at the minute its party leaves. I’d write tests for those first: every rule with a number in it, tried on both sides of the number.

A test for the last-table race would have to start two threads and hope they collide, so it would only catch the bug on some runs. I’ve left it until chapter 21, where the race is run against a real database.

6.6. ScalaTest

MUnit dates from 2020 and ScalaTest from Scala’s early years. You might also come across specs2, uTest, Weaver and ZIO Test, or plain JUnit. The two tests of Party below are written for ScalaTest. You don’t need to type them in. They’re in the book’s downloadable code under ch06/scalatest, in a directory of their own with org.scalatest::scalatest:3.2.20 as the test dependency:

package fullybooked

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class PartySpec extends AnyFlatSpec, Matchers:
  "A party of four" should "stay ninety minutes" in {
    Party("Abara", 4).stayMinutes shouldBe 90
  }

  "A party of six" should "pay a deposit of ten pounds a head" in {
    Party("Haddad", 6).deposit shouldBe BigDecimal(60)
  }

The style here, AnyFlatSpec, is one of several in ScalaTest. It has you write each test as a sentence, in which should, in and shouldBe are ordinary methods called without dots or parentheses. shouldBe comes from Matchers, along with a long list of assertions that read like it. Another style, AnyFunSuite, looks much like MUnit. Two ScalaTest codebases written in different styles can look quite unlike each other, so when you join one, follow the style that’s already there.

These tests run with the same command as MUnit’s, scala test ..

6.7. Exercises

Solutions are in the book’s downloadable code, under ch06/exercises.

  1. Write three tests for findBooking: a name that’s in the list, a name that isn’t, and a name with the wrong capitals.

  2. Test leaveBy for a party of four and a party of five, both arriving at seven.

  3. intercept[IllegalArgumentException]: followed by an indented block passes when the block throws that exception and fails when it doesn’t. Use it to test that Status.valueOf("Gone") is rejected.

  4. Test label for a party of six, once with a confirmed booking and once with a requested one.

  5. The Patels, two people on table 1, ask to swap with the Abaras, four people on table 4. Write a test that says what swapTables should return, watch it fail, then fix swapTables. In a for laid out over several lines, an if can go on a line of its own after the <- clauses.

  6. Mark one test in AvailabilityTests with .only and run the suite with -v. Then change it to .ignore. What does MUnit print each time?