Testing the System at Multiple Levels

Why a passing API test is not enough: unit, database, contract, messaging and end-to-end tests, financial edge cases, concurrency, and coverage without false confidence.

  • Advanced
  • About 23 minutes
  • Includes an exercise and a knowledge check

The concept A successful response is not enough

A system can pass a basic endpoint test and still be wrong. The payment is saved, but its event is never published. The event is published, but processed twice. A match is found, but the database lets a second payment claim the same settlement. The UI shows success, but no audit record exists. Tests must cover business behavior across system boundaries.

On the left, a photo of crash test dummies in a vehicle safety laboratory. On the right, five test levels: unit, for matching rules, adapters, money parsing and case transitions, many and fast; database integration on real PostgreSQL, for constraints, migrations and concurrent inserts; API contract, for schemas, validation, authentication, authorization and errors; messaging, for outbox recovery, duplicate delivery, retry, dead letters and consumer restart; end to end, from payment to match or exception through the UI with audit history, few but complete.
Crash it on purpose, before the customer does.

Level 1 Unit tests verify behavior

javaA unit test that checks the outcome and the evidence (JUnit 5)

@Test
void returnsAmbiguousWhenTwoSettlementsFitEqually() {
    var payment = payment("PAY-77840", "REF-900200", 25000);
    var open = settlements(
            settlement("STL-503", "REF-900200", 25000),
            settlement("STL-504", "REF-900200", 25000));

    Result result = engine.reconcile(payment, open, NOW);

    assertEquals(Status.AMBIGUOUS, result.status());
    assertNull(result.settlementId());                 // nothing was claimed
    assertTrue(result.evidence().contains("STL-503"));
    assertTrue(result.evidence().contains("STL-504"));
    assertEquals(2, open.size());                      // both still available
}

A useful unit test checks behavior, not that a method was called. It is named after the business rule, uses realistic values and asserts the outcome, the evidence and what did not change.

Levels 2 to 5 Integration, contract, messaging and end to end

LevelVerifyHow
Database integrationPersistence, unique constraints, foreign keys, transactions, migrations, query correctness, concurrent operationsA real PostgreSQL in a container (for example with Testcontainers), not mocked repositories
API contractRequest and response schemas, validation, authentication, authorization, error format, version compatibilityContract tests against the OpenAPI document and each provider’s agreed examples
MessagingPublication, consumption, duplicate delivery, retry, dead letters, outbox recovery, consumer restartA real broker container; kill the consumer mid-message on purpose
End to endWhole business workflows across components, including the UIA browser test driving the real stack with fictional test data
End-to-end scenarioSteps
Happy pathSubmit a payment; confirm it is stored and its event published; submit the settlement; run reconciliation; find the transaction in the UI and see the matching evidence
Exception pathSubmit a payment and a settlement with a different amount; confirm an exception opens; assign it; add notes; submit; approve as a different supervisor; confirm the audit history shows every action

Most defects that cost money live in the negative paths. These tests must exist before anyone calls the slice production-grade:

Negative testExpected result
Concurrent duplicates: 20 threads send the same notification at onceExactly one payment row and one outbox event; 19 answers are 200 with the same ingestion ID
Transaction rollback: force the outbox insert to failNeither the payment nor the event is stored; the provider gets a 503 and retries
Outbox redelivery: crash the publisher after sending, before markingThe event is published twice; the consumer applies it once (processed_event)
Malformed files: truncated, unreadable trailer, wrong record count, non-UTF-8 bytesBatch FAILED or COMPLETED_WITH_ERRORS with line numbers; nothing silently accepted
Authorization bypass: call the approval API directly as an analyst, with the button hidden403; nothing changes; a security log entry. Hiding a button is not authorization
Maker-checker: the maker tries to approve their own request403 from the backend, whatever the UI shows; the request stays PENDING_APPROVAL
Maker-checker: approve a request that was already approved or rejected409; the outcome does not change twice
Signature: wrong key, altered body, old timestamp, replayed request401 each time; nothing stored

Beware Coverage is not correctness

Left, an example coverage report of 92 percent: every line ran, but no test sent the same payment twice, so a duplicate financial effect ships. Right, tests named after business risks: storesOnePaymentWhenSameNotificationArrivesFiveTimes, rejectsSameTransactionIdWithDifferentAmount, neverMatchesOneSettlementToTwoPayments, keepsPaymentAwaitingSettlementWhenFileIsLate, refusesApprovalBySubmitter, reportsRejectedLinesAndKeepsBatchTotals. Below, edge cases to include: zero, negative and large amounts, another currency, duplicate provider reference, missing loan account, delayed and duplicate settlement, multiple candidates, concurrent ingestion, invalid file, replayed event and database interruption.
Coverage shows which lines ran, not which risks were checked.

Practice Build the test strategy

Practical exercise

Exercise 10: automated tests and a test evidence report

Write the suite and a one-page report of what it proves.

  1. Unit and database integration tests.
  2. API contract and messaging tests.
  3. End-to-end, concurrency and security tests.
  4. Financial edge-case tests.
Show a sample evidence report entry
  • Risk: duplicate financial effect from repeated notifications.
  • Tests: 20 concurrent identical requests (database integration); redelivered event (messaging); end-to-end repeat through the API.
  • Result: one payment, one outbox event, one reconciliation result in every run.
  • Not yet covered: duplicates across a provider’s regions; waiting for the provider to confirm its ID scope.

3 questions

Check your understanding

1. Which test best shows that a duplicate event does not create a second effect?
2. What is an end-to-end test for?
3. A suite has 95% coverage. What does that prove?
Answer every question to submit.

What you can do now

  • Choose the right test level for each risk.
  • Write behavior-focused unit tests.
  • Test with real databases, brokers and failures.
  • Report test evidence without false confidence.