Test Strategy for an Order Platform: Unit, Integration, Contract, Failure, and E2E
A test strategy for an order platform that uses Kafka, Connect, schemas, and databases must cover five layers: unit tests, integration tests, contract tests, failure tests, and end-to-end tests. Each layer has a different scope and cost, and the strategy is to use the cheapest layer that can catch a given class of bug. Unit tests verify business logic in isolation, with mocks for Kafka and the database. They are fast and numerous. Integration tests verify the application against a real Kafka broker (via Testcontainers) and a real database (via Testcontainers or an in-memory database). They catch serialization, offset management, and SQL issues. Contract tests verify that producers and consumers agree on the schema and the semantics of the events. Failure tests verify behavior when the broker, database, or downstream service is unavailable. End-to-end tests verify the full flow from order creation to fulfillment, including Kafka, Connect, and the database. The strategy is to have many unit tests, a moderate number of integration and contract tests, a focused set of failure tests, and a small number of end-to-end tests that run before release.
The mechanism for each layer is different. Unit tests use Mockito or similar to mock the Kafka client and the repository. Integration tests use Testcontainers to start Kafka, Schema Registry, and PostgreSQL (or the actual database). They produce records to a topic, run the consumer or Kafka Streams topology, and assert on the database state. Contract tests use the schema registry's compatibility check and consumer-driven contract testing (e.g., Pact or Spring Cloud Contract) to verify that the producer's events match what the consumer expects. Failure tests use Toxiproxy to inject network failures or stop containers to simulate outages, then verify that the consumer recovers, does not lose records, and remains idempotent. End-to-end tests use a staging environment with the full stack: a producer writes an order to Kafka, Kafka Connect writes it to the database, and a verification step checks the database and the downstream topics. The trade-off is between coverage and cost. End-to-end tests give the most confidence but are the slowest and most brittle. Unit tests are fast but do not catch integration bugs. The strategy should be layered so that most bugs are caught by the cheaper layers, and the expensive layers are reserved for the highest-risk scenarios.
A common mistake is to rely only on end-to-end tests. They are slow, flaky, and hard to debug, and they do not tell you where the bug is. Another mistake is to skip failure tests because they are hard to write; failures are exactly where order platforms break. A third mistake is to test Connect connectors only manually. Connect connectors should be tested with Testcontainers and a real database, including the schema evolution and offset recovery paths. The trade-off is between test maintenance and confidence. A large test suite requires maintenance, and flaky tests erode trust. The strategy should include a process for quarantining flaky tests and for adding a regression test for every production incident. Version note: Testcontainers supports Kafka, Schema Registry, Connect, and databases, so you can build a full-stack integration test in a single JVM test. For Kafka Streams, TopologyTestDriver is a fast alternative for topology logic, but it does not exercise the broker. For Connect, Testcontainers can run a Connect worker and a connector, which is the only way to test connector behavior realistically.
Five layers: unit, integration, contract, failure, and end-to-end tests.
Unit tests mock Kafka and the database; they are fast and numerous.
Integration tests use Testcontainers for Kafka, Schema Registry, and the database.
Contract tests verify schema compatibility and event semantics.
Failure tests use Toxiproxy or container stops to verify recovery and idempotency.
End-to-end tests run the full stack in staging and are few but high-value.
Test Connect connectors with Testcontainers and a real database, including schema evolution and offset recovery.
Add a regression test for every production incident; quarantine flaky tests.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience