Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 39 ยท From Models to Code

Sequence Diagrams to Tests

Use UML sequence diagrams as executable specifications: map messages to calls, lifelines to objects, and alt/loop fragments to test cases with mocks.

A sequence diagram tells you who calls whom, in which order, and under which conditions. That is exactly the information an interaction test needs. When you can turn the diagram into a test, the diagram stops being a drawing that rots and becomes an executable promise: if the code stops doing what the diagram says, a test fails.

This lesson shows how to read a sequence diagram as a test script, how to use fakes and mocks for the other lifelines, and how to cover the alt, opt and loop fragments.

Reading a diagram as a test

Take a checkout scenario.

sequenceDiagram
    actor U as Customer
    participant C as CheckoutService
    participant P as PaymentGateway
    participant S as StockService
    participant M as Mailer
    U->>C: checkout(cart)
    C->>S: reserve(items)
    S-->>C: ok
    C->>P: charge(total)
    alt approved
        P-->>C: approved
        C->>M: sendReceipt(user)
        C-->>U: confirmation
    else declined
        P-->>C: declined
        C->>S: release(items)
        C-->>U: error
    end

The diagram contains one system under test (CheckoutService) and three collaborators.

The mapping rules:

Diagram Test
Lifeline you are specifying The system under test (SUT): a real object
Other lifelines Test doubles: fakes, stubs or mocks
Message into the SUT The action in the test (the call you make)
Message out of the SUT An expectation: the mock should have received this call
Return message What a stub returns
Message order Order of the verification
alt / opt One test per branch
loop A test with 0, 1 and several iterations
break A test where the early exit happens
Actor The test itself

The result is one test per path through the diagram. The diagram above gives two tests: approved and declined.

Stubs, fakes and mocks

A stub returns canned answers. A fake is a working but simple implementation (an in-memory database). A mock records calls and lets you verify them. For interaction tests, you need a recording double: it logs every call with its arguments so the test can compare the log to the diagram. Frameworks such as Mockito, unittest.mock and Jest automate this, but a hand-written recorder is enough to learn the idea.

A recording double and the happy path

Python Test the approved branch: messages as expectations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the expected list is the diagram's arrows, top to bottom, restricted to messages that leave the SUT. If a developer inserts a call or swaps the order, the test fails and points back at the model.

The declined branch

The second path of the alt is a separate test with a different stub answer.

Python Test the declined branch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The absence of a message matters as much as its presence: the diagram says no receipt is sent when the card is declined.

Fragments and test cases

Fragment What to test
alt [a] ... else [b] One test per operand, including else
opt [condition] Once with the condition true, once false
loop [n] or loop [while x] Zero, one, many iterations; the boundary of the guard
par Interleavings; usually test each branch plus a concurrency stress test
break The early exit path
ref The referenced interaction has its own tests; mock it here

Derive the cases by counting paths. For a diagram with two independent alt fragments, you have four combinations; decide which of them matter. A tool such as decision table testing helps you trim the list.

Sequence-level checks beyond order

  • Arguments: the diagram says charge(total); the test should verify the amount, not just that charge was called.
  • Return values drive the branches; stub them from the diagram's return arrows.
  • Timing: asynchronous messages (open arrowheads) mean the SUT does not wait for the answer; test that it does not block.
  • Creation and destruction: a message to a lifeline's header creates the object; an X at the lifeline's end means it is destroyed.

Do not over-specify

Strict interaction tests are brittle: if you assert every internal call in every test, a harmless refactoring breaks dozens of them. Use them at architecture boundaries (the SUT and its ports: gateway, mailer, repository) and use state or output tests inside the module. A sequence diagram that is only used for the interaction between components, not for every private call, has the same sweet spot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a sequence diagram used for an interaction test, what do messages leaving the system under test become?

    Outgoing messages are what the SUT must send; a recording double verifies them.
  2. 2. An alt fragment has two operands. How many tests do you need at least?

    One test per operand covers every branch.
  3. 3. What is the difference between a stub and a mock?

    Stubs supply data; mocks check interactions.
  4. 4. Why not assert every internal call in every test?

    Verify interactions at boundaries and use state or output checks inside modules.

Summary

  • A sequence diagram's SUT lifeline is real; the other lifelines become test doubles.
  • Incoming messages are the actions, outgoing ones are expectations, returns are stubbed answers.
  • One test per path: each alt, opt, loop and break produces cases.
  • Check arguments and the absence of calls, not only the order.
  • Use interaction tests at boundaries to avoid brittle suites.

Next: Reverse Engineering and Round-Trip