Course contentsShow
← Software Engineering
Lesson 471 of 1,93314. Test DoublesFree lesson

Test Double Libraries and Frameworks

Exploring popular mocking frameworks and their APIs for creating, configuring, and verifying test doubles efficiently.

Test Double Libraries and Frameworks

You'll learn: How mocking frameworks simplify creating and managing test doubles with convenient APIs.

The Manual Approach Problem

So far, you've learned about dummies, stubs, fakes, spies, and mocks. You could hand-write each one as a custom class for every test scenario—but imagine creating dozens of spy objects, each tracking different method calls. That's tedious and error-prone.

What Mocking Frameworks Do

Mocking frameworks are specialized libraries that let you create test doubles on-the-fly using a simple API, rather than writing full classes. Think of them as a "test double factory" that builds exactly what you need with just a few lines of configuration.

Popular frameworks include:

  • JavaScript: Jest (built-in), Sinon.js
  • Python: unittest.mock, pytest-mock
  • Java: Mockito, JMock
  • C#: Moq, NSubstitute

How They Work

These frameworks typically let you:

  1. Create doubles quickly (usually mocks or spies)
  2. Configure their behavior (what they return when called)
  3. Verify interactions (what was called, how many times, with which arguments)

Instead of manually tracking method calls in a hand-written spy, you use the framework's built-in verification methods. Instead of writing a stub class with hardcoded return values, you tell the framework: "when getUser() is called, return this object."

The Trade-off

Frameworks make tests faster to write and more expressive, but you're learning a new API. Each framework has its own syntax and capabilities. The investment pays off when you're writing many tests that need doubles.

Key Takeaway:
Test double frameworks provide APIs to quickly create, configure, and verify test doubles, eliminating the need to manually write stub and mock classes for every test scenario.