0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Matchers

Expressive assertions.

Matchers is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are Matchers?

ScalaTest matchers provide an expressive, English-like syntax for assertions. Instead of assert(x == 3), you write x shouldBe 3, producing readable tests and clear failure messages.

Mixing in Matchers

Mix the Matchers trait into your spec to unlock the DSL. It combines with any testing style.

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

class DemoSpec extends AnyFlatSpec with Matchers {
  "A value" should "equal itself" in {
    val x = 3
    x shouldBe 3
  }
}

Equality Matchers

Check equality with shouldBe or should equal. Both compare with == but read naturally.

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

class EqSpec extends AnyFlatSpec with Matchers {
  "sum" should "be correct" in {
    (2 + 2) shouldBe 4
    (2 + 2) should equal (4)
  }
}

Comparison Matchers

Numeric comparisons read like prose: should be >, should be <=, and ranges with be within for floating point tolerance.

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

class CmpSpec extends AnyFlatSpec with Matchers {
  "a number" should "compare" in {
    7 should be > 3
    7 should be <= 7
    2.0 shouldBe 2.0 +- 0.01
  }
}

String Matchers

Matchers for strings include startWith, endWith, include, and regex with fullyMatch regex.

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

class StrSpec extends AnyFlatSpec with Matchers {
  "a string" should "match parts" in {
    "hello world" should startWith ("hello")
    "hello world" should include ("o w")
    "hello world" should endWith ("world")
  }
}

Collection Matchers

Inspect collections fluently: have size, contain, contain allOf, and emptiness with shouldBe empty.

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

class ColSpec extends AnyFlatSpec with Matchers {
  "a list" should "have expected contents" in {
    List(1, 2, 3) should have size 3
    List(1, 2, 3) should contain (2)
    List.empty[Int] shouldBe empty
  }
}

Boolean and Option Matchers

Match truthiness and Option contents directly: shouldBe true, shouldBe defined, and should contain for the inner value.

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

class OptSpec extends AnyFlatSpec with Matchers {
  "an option" should "be inspected" in {
    Some(5) shouldBe defined
    Some(5) should contain (5)
    None shouldBe empty
  }
}

Combining Matchers

Combine expectations with and / or for compound assertions.

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

class CombineSpec extends AnyFlatSpec with Matchers {
  "a number" should "satisfy both" in {
    7 should (be > 0 and be < 10)
  }
}

Testing for Exceptions

Use an [Exception] should be thrownBy { ... } or assertThrows to verify that code fails as expected.

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

class ExSpec extends AnyFlatSpec with Matchers {
  "dividing by zero" should "throw" in {
    an [ArithmeticException] should be thrownBy (1 / 0)
  }
}

Why Matchers Help

Beyond readability, matchers produce descriptive failure messages. A failed List(1,2) should have size 3 reports the expected and actual size, unlike a bare assert that only says the boolean was false.

shouldBe vs should equal

They are nearly identical; shouldBe is terser and gives slightly cleaner messages for simple equality, while should equal supports custom Equality instances for advanced comparisons.

Quick Check

Test your knowledge of matchers.

Recap

You learned ScalaTest matchers:

  • Mix in Matchers for the DSL.
  • Equality: shouldBe, should equal; comparisons with be >, +- tolerance.
  • Strings: startWith, include; collections: have size, contain.
  • Exceptions: should be thrownBy.
  • Matchers give clear, descriptive failures.

Frequently asked questions

Is the “Matchers” lesson free?

Yes — the full text of “Matchers” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Matchers”?

Expressive assertions. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Matchers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ScalaTest Styles
  2. Matchers
  3. Property-Based Testing
  4. Mocking and Fixtures
← Back to Scala for Backend Engineering & Functional Programming