camel-component-mock
Mock component for rust-camel testing
Overview
The Mock component is a testing utility that records every exchange it receives. It's essential for writing unit tests and integration tests for your routes.
This is a producer-only component - it records exchanges sent to it and provides assertions for testing.
Features
- Record all received exchanges
- Assert exchange count
- Inspect recorded exchanges
- Shared state across endpoint instances
- Perfect for testing route outputs
Installation
Add to your Cargo.toml:
[]
= "0.2"
URI Format
mock:name
Usage
Basic Testing
use RouteBuilder;
use MockComponent;
use CamelContext;
async
Integration Test Example
use RouteBuilder;
use MockComponent;
use DirectComponent;
use CamelContext;
async
Multiple Endpoints
let mock = new;
let route = from
.multicast
.to
.to
.end_multicast
.build?;
// Later, assert on both
let endpoint_a = mock.get_endpoint.unwrap;
let endpoint_b = mock.get_endpoint.unwrap;
endpoint_a.assert_exchange_count.await;
endpoint_b.assert_exchange_count.await;
MockEndpointInner Methods
| Method | Description |
|---|---|
get_received_exchanges() |
Get all recorded exchanges |
assert_exchange_count(n) |
Assert exact count (panics if mismatch) |
await_exchanges(n, timeout) |
Async wait until at least n exchanges arrive or timeout |
exchange(idx) |
Get an ExchangeAssert for the exchange at index idx |
ExchangeAssert (fluent assertions)
endpoint
.await_exchanges
.await;
endpoint
.exchange
.assert_body_text
.assert_header
.assert_no_error;
| Method | Description |
|---|---|
assert_body_text(expected) |
Assert body is Body::Text with the given value |
assert_body_json(expected) |
Assert body is Body::Json matching the given serde_json::Value |
assert_body_bytes(expected) |
Assert body is Body::Bytes with the given bytes |
assert_header(key, value) |
Assert a header equals the given JSON value |
assert_header_exists(key) |
Assert a header is present |
assert_has_error() |
Assert the exchange carries an error |
assert_no_error() |
Assert the exchange has no error |
Best Practices
- Clone the MockComponent before registering to keep a reference for assertions
- Use descriptive names for mock endpoints to make tests readable
- Assert early - check exchange count before inspecting contents
- Clean shutdown - stop the context before assertions to ensure all exchanges are processed
Documentation
License
Apache-2.0
Contributing
Contributions are welcome! Please see the main repository for details.