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.
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
| Level | Verify | How |
|---|---|---|
| Database integration | Persistence, unique constraints, foreign keys, transactions, migrations, query correctness, concurrent operations | A real PostgreSQL in a container (for example with Testcontainers), not mocked repositories |
| API contract | Request and response schemas, validation, authentication, authorization, error format, version compatibility | Contract tests against the OpenAPI document and each provider’s agreed examples |
| Messaging | Publication, consumption, duplicate delivery, retry, dead letters, outbox recovery, consumer restart | A real broker container; kill the consumer mid-message on purpose |
| End to end | Whole business workflows across components, including the UI | A browser test driving the real stack with fictional test data |
| End-to-end scenario | Steps |
|---|---|
| Happy path | Submit 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 path | Submit 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 test | Expected result |
|---|---|
| Concurrent duplicates: 20 threads send the same notification at once | Exactly one payment row and one outbox event; 19 answers are 200 with the same ingestion ID |
| Transaction rollback: force the outbox insert to fail | Neither the payment nor the event is stored; the provider gets a 503 and retries |
| Outbox redelivery: crash the publisher after sending, before marking | The event is published twice; the consumer applies it once (processed_event) |
| Malformed files: truncated, unreadable trailer, wrong record count, non-UTF-8 bytes | Batch FAILED or COMPLETED_WITH_ERRORS with line numbers; nothing silently accepted |
| Authorization bypass: call the approval API directly as an analyst, with the button hidden | 403; nothing changes; a security log entry. Hiding a button is not authorization |
| Maker-checker: the maker tries to approve their own request | 403 from the backend, whatever the UI shows; the request stays PENDING_APPROVAL |
| Maker-checker: approve a request that was already approved or rejected | 409; the outcome does not change twice |
| Signature: wrong key, altered body, old timestamp, replayed request | 401 each time; nothing stored |
Beware Coverage is not correctness
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.
- Unit and database integration tests.
- API contract and messaging tests.
- End-to-end, concurrency and security tests.
- 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
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.