Test a Service
Domain tests check the decisions and the processors (Test Domain Code). A service test checks the rest: that the host builds the service from its modules and its configuration, that its routes answer, that its sagas and schedules run, and that it works on the stores it will use in production. This page tests the eShop's ordering service at three levels.
| Level | Runs | Proves |
|---|---|---|
| In process | The service in the test's JVM, on the memory backend, topics in memory | The assembly, the routes, the sagas, the waits, the timers |
| On real stores | The same, on PostgreSQL and ClickHouse in containers | The schema, the queries, the sinks |
| The contract | The build's merged Manifest against the committed baseline | No change breaks a client, and the baseline records every change |
Run the service in the test
A service test starts the application with wowServer<App>(profiles…) from wow-test-spring-boot, on random ports and loopback addresses, and talks to it over HTTP through its http client:
private val server = wowServer<OrderingServer>(
"dev",
properties = mapOf(
"wow.timers.poll-interval" to "200ms",
"eshop.ordering.payment-grace-period" to "2s",
),
)- The fixture binds both ports on the loopback interface at free ports. That states nothing of development: a test server runs as its profiles say, as a deployment does. In a development profile (
dev, as above) and without a store bean, the node runs on the memory backend, which serves the snapshot and events queries from memory unlesswow.query.memory.enabledisfalse. Without one, it refuses what a deployment refuses: no durable backend, noCallerResolver, topics in memory. - It shortens the timeouts a node waits on at its close (
wow.cluster.drain-timeout,wow.subscription.stop-timeout), so the shutdown between test classes is quick;propertiesadd the test's own, herewow.timers.poll-interval=200ms, which fires scheduled commands promptly. - The application's own settings shorten what the test waits for: here the payment grace period, two seconds instead of thirty minutes.
The ordering's sagas send the inventory's commands, so the node needs a command transport. A test server in a development profile needs nothing for it: wow.transport=auto keeps its command topics and published streams in its JVM unless the application configures a transport. The kit adds no profile of its own: a server started without one has no topics in memory. A test that writes to the topics before the node starts, such as another context's published stream a saga reads from its beginning, or servers that share topics, gives them with topics:
@Test
fun `servers given the same topics should share them, which outlive each`() {
val topics = InMemoryTopics(4)
wowServer<BankServer>("dev", topics = topics).use { first ->
first.bean<InMemoryTopics>().assert().isSameAs(topics)
runBlocking { first.commands(Caller.ANONYMOUS).wait(OpenAccount("Bea"), "bea") }
wowServer<BankServer>("dev", topics = topics).use { second ->
second.bean<SourceTopics>().source.assert().isSameAs(topics)
}
}
eventually("the account's stream published") { topics.messages(ACCOUNTS).size == 1 }
}- The node takes the topics it is given in place of its own, with
wow.transport=memory, and leaves them open when it closes, so they outlive each server that shares them. Topics in memory are a development node's alone: a server giventopicswithout a development profile refuses to start, namingwow.dev-profiles; the kit adds no profile by itself. server.bean<InMemoryTopics>()is a node's topics, given or its own, to read what its sagas sent and its streams published.
A test then drives the service with its typed clients (Send and read as a caller): it waits to the end of a saga's chain where a wait plan reaches it, and polls for what none does. An order whose stock is confirmed and that is not paid cancels itself when its scheduled command is due:
@Test
fun `an order not paid within its grace period should cancel itself`() {
submit("order-unpaid")
confirm("order-unpaid", "mug", "cap")
order("order-unpaid").status.assert().isEqualTo(OrderStatus.STOCK_CONFIRMED)
// The order cancels itself by a scheduled command of its own, which no command of the test's waits for.
eventually("order-unpaid cancelled") { order("order-unpaid").status == OrderStatus.CANCELLED }
order("order-unpaid").cancelReason.assert().contains("payment grace period")
assertRejected { orders().wait(PayOrder("p-2", BigDecimal("29.00")), "order-unpaid") }
.code.assert().isEqualTo("eshop-ordering.order.wrong-status")
}Send and read as a caller
The server's typed clients send the commands of the service's api module and read its states, as a caller, with no path and no JSON in the test: commands(caller) is a CommandClient and queries(caller) the caller's Queries, both on the server's public port. They are wow-client's, the client a service calls its peers with, in user mode. The caller goes as the development identity headers (Wow-User, Wow-Owner, Wow-Roles, Wow-Tenant, Wow-App: UserCredential.DEVELOPMENT), which the host reads in a development profile when the application has no CallerResolver and CoSec is off (Who the caller is), so the test starts the server in one, dev. The quickstart's bank service, tested so:
@Test
fun `the typed clients should send commands and read states as the caller`() {
val alice = Caller.owner("alice")
val commands = server.commands(alice)
runBlocking {
// wait answers only a result that succeeded: a rejection is thrown.
commands.wait(OpenAccount("Alice"), "alice")
commands.wait(Deposit(100), "alice").version.assert().isEqualTo(2)
val account = server.queries(alice).snapshots<AccountState>().state("alice")!!
account.state.balance.assert().isEqualTo(100L)
account.operator!!.id.assert().isEqualTo("alice")
}
assertRejected { commands.wait(Withdraw(150), "alice") }
.code.assert().isEqualTo("bank.account.insufficient-balance")
}waitanswers with the result the command's wait plan ends with, which may wait to the end of a saga's chain. A rejection, by the aggregate or at admission (an invalid or unknown command, a forbidden caller), is aCommandRejectedExceptionwith its error code; any other problem aWowProblemException. A test expects a rejection withassertRejected { … }, which answers it and fails when the command was answered otherwise; its block may call the clients'suspendfunctions.- A system command goes the same way, by its class, to the aggregate type the request names: a lifecycle command (
DeleteAggregate().toCommandRequest(id) { type(…) }), a snapshot rebuild (RebuildSnapshot.of(ids)), a job's command (DisableJob, to<context>.scheduleby the job's name). - A server that authenticates its callers otherwise, behind CoSec or with a
CallerResolverof its own, takes the credential it reads:commands(caller, credential)andqueries(caller, credential), aUserCredentialthat writes the caller as that server's headers. Behind the CoSec gateway (wow.cosec.mode: gateway) that is the identity the gateway forwards, whichwow-cosec'sCoSecIdentitywrites as CoSec documents it:UserCredential { caller -> CoSecIdentity(caller.subject!!, listOf("buyer"), tenant = caller.tenantId).headers() }, which carries the caller it is given: its subject and tenant, and its app and space (app = caller.app.orEmpty(),space = caller.scope.spaceId.orEmpty()) for a caller that has them. It is experimental with the module (@OptIn(ExperimentalWowApi::class)). - What no wait plan sees, a scheduled command that fires or a call another service received, a test polls with
eventually("what") { condition }, every 100 milliseconds for at most 20 seconds by default. A condition that throws, an assertion among them, has not held yet; the failure says what was not reached, with thedetailgiven (the services' output, say) and the condition's last failure as its cause. server.httpstays for the wire itself: a route's shape, a header, a problem's members. A request names its caller as the typed clients do (server.http.post(path, json, caller = alice), the development identity headers).Answer.problemreads anapplication/problem+jsonanswer as a typedProblem: its status, code and violations, the rejected command's result, the aggregate's version, and what a retry may do;Answer.resultreads a command's result, a success's or a problem's.
@Test
fun `a problem the server answered should be typed`() {
server.http.post("/bank/account/carol/open-account", """{"owner":"Carol"}""").status.assert().isEqualTo(200)
val invalid = server.http.post("/bank/account/carol/deposit", """{"amount":0}""").problem!!
invalid.status.assert().isEqualTo(400)
invalid.code.assert().isEqualTo("wow.command.invalid")
val violation = invalid.violations.single()
violation.path.assert().isEqualTo("amount")
violation.code.assert().isEqualTo("positive")
val rejected = server.http.post("/bank/account/carol/withdraw", """{"amount":1}""").problem!!
rejected.status.assert().isEqualTo(409)
rejected.code.assert().isEqualTo("bank.account.insufficient-balance")
rejected.result!!.status.assert().isEqualTo(CommandResult.Status.REJECTED)
rejected.version.assert().isEqualTo(1)
rejected.retry.assert().isEqualTo(RetryAdvice.NONE)
}Fake the services it calls
A saga that calls another service synchronously, a command to that service's internal port, is tested without the service: fakePeer(context) stands in for it, built from the contract its api module carries on the test's class path, and wowServer(…, peers = listOf(peer)) points the server at it (wow.peers.<context>.url). The fake records each call it takes: the command, decoded to its class, the service that sent it, and the root command the call acts for, with its tenant, scope and operator. The basket's checkout calls the ordering so:
fakePeer("eshop-ordering").use { ordering ->
wowServer<BasketServer>("dev", peers = listOf(ordering)).use { basket ->
val alice = Caller.owner("alice")
runBlocking { basket.commands(alice).wait(CheckoutBasket, "alice") }
val call = ordering.called<SubmitOrder>()
call.service.assert().isEqualTo("eshop-basket-service")
call.operator!!.id.assert().isEqualTo("alice")
call.scope.assert().isEqualTo(Scope(ownerId = "alice"))
(call.command as SubmitOrder).items.single().productId.assert().isEqualTo("mug")
}
}called<C>()takes the first call of the command classCthat it has not given back yet, waiting at most 20 seconds;callslists them all.- The fake answers each call as
answer { call -> … }says.PeerAnswer.succeeded(id, version, results), the default, commits the command at version 1 to the call's aggregate, or to a new one for a creation that names none, with the values its decision answered.PeerAnswer.rejected(code, message, status, violations, extensions)answers with the status the peer's contract gives the code: its error tables, else 422 for a domain rejection.PeerAnswer.failed(code, message)answers a failure of the peer's node, a code of the error catalog with its status and what a retry may do.PeerAnswer.timedOut()answers 504wow.wait.timeout, which the caller may send again with the same request id. - It refuses a request that names no service (
Wow-Service) or no root command (Wow-Call) with 403wow.auth.forbidden, as a peer's internal port does. - It refuses a command the calling service may not send, as the peer's internal port does: 403
wow.call.command-forbidden, the call not recorded. What each service may send is the peer'swow.callers.<service>.commands, given asfakePeer(context, callers = mapOf("eshop-basket-service" to listOf("wow.lifecycle.DeleteAggregate"))). With none, the default, every domain command is taken and every system command (wow.*, a lifecycle command among them) is refused: a saga that calls a peer's system command states the peer's permit, as the peer's configuration must. - It refuses a body that is not JSON, gives a property twice, or does not read as its command's class with 400
wow.command.malformed, as the peer does. It checks none of the command's constraints: a body the peer refuses aswow.command.invalidis taken.
Answer as the peer would
Code that calls another service handles three answers: a success, whose values its decision answered (CommandResult.results); a rejection, a CommandRejectedException with its code and the fields the command broke (violations); and a failure, a WowProblemException that says whether a retry may get past it. A test gives each by name, without opting into Wow's internals, from either fake.
In a unit test, the code takes the CommandClient it calls the peer with, and the test gives it an answeringCommandClient of wow-test (Testing):
/** Reserves [sku] at the inventory and says how it went: the code under test, which calls a peer. */
suspend fun reserve(inventory: CommandClient, sku: String): String = try {
val result = inventory.wait(Reserve(sku).toCommandRequest(to = STOCK.ref("acme", sku)))
"reserved as ${result.results["reservationId"]}"
} catch (rejected: CommandRejectedException) {
"refused: ${rejected.code} at ${rejected.violations.map { it.path }}"
} catch (problem: WowProblemException) {
if (problem.retryable) "try again later" else throw problem
}val inventory = answeringCommandClient { call ->
when ((call.request.command as Reserve).sku) {
"mug" -> call.committed(results = mapOf("reservationId" to "R-1"))
"x" -> call.rejected(
"inventory.stock.unknown-sku",
violations = listOf(Violation.of("sku", "unknown", "No product has this SKU.")),
)
else -> call.failed(FrameworkCodes.STORAGE_UNAVAILABLE)
}
}
runBlocking { reserve(inventory, "mug") }.assert().isEqualTo("reserved as R-1")
runBlocking { reserve(inventory, "x") }.assert().isEqualTo("refused: inventory.stock.unknown-sku at [sku]")
runBlocking { reserve(inventory, "cup") }.assert().isEqualTo("try again later")call.committed(version, ref, results)is the success, with its values.call.rejected(code, message, ref, violations, extensions)is thrown fromsendandwaitas theCommandRejectedExceptiona real client throws.call.failed(code, message)is thrown fromsendandwaitas theWowProblemExceptionof a call that failed, which a retry with the same request id may get past. To give another status or retry, the lambda throws aWowProblemExceptionof its own.streamemits each answer as a result, whatever its status, rejected and failed ones included, as a real client's stream emits every status.call.requestIdis the request id the call was sent under. A call a decide function sends without one takes the id a node's client derives for it: the same at every attempt of the command, and a call that changes at a later attempt fails the command, as on a node (Undo an outside act). A call sent elsewhere keeps its own, or none.
Against the server, fakePeer answers the same three over HTTP, as the peer's routes would, and the server's client reads them back:
shipping.answer { PeerAnswer.succeeded(results = mapOf("trackingNo" to "TN-1")) }
runBlocking { commands(alice).wait(ShipParcel("1 Main St"), "parcel-1") }
.results["trackingNo"].assert().isEqualTo("TN-1")
shipping.answer {
PeerAnswer.rejected(
"shipping.parcel.no-address",
violations = listOf(Violation.of("address", "unknown", "No such street.")),
)
}
assertRejected { commands(alice).wait(ShipParcel("9 Nowhere"), "parcel-2") }
.violations.single().path.assert().isEqualTo("address")
shipping.answer { PeerAnswer.failed(FrameworkCodes.STORAGE_UNAVAILABLE) }
val failed = assertThrows<WowProblemException> {
runBlocking { commands(alice).wait(ShipParcel("1 Main St"), "parcel-3") }
}
failed.retryable.assert().isTrue()Run it on real stores
The memory backend answers the snapshot and events queries from memory, by the in-memory engine, and writes no warehouse. A test of a store's schema, of its own query engine and of the warehouse runs the service on the stores themselves, started by Testcontainers. me.ahoo.wow.service registers a test suite of its own for them, integrationTest, which check does not run, so the unit tests stay fast and need no Docker: its sources go under src/integrationTest/kotlin, and ./gradlew integrationTest runs them. The suite runs on JUnit Jupiter, against the module's classes and everything testImplementation gives; the plugin adds nothing else, so the build script declares what the tests need, Testcontainers among them:
testing {
suites {
named<JvmTestSuite>("integrationTest") {
dependencies {
implementation(platform("org.testcontainers:testcontainers-bom:2.0.5"))
implementation("org.testcontainers:testcontainers-postgresql")
}
}
}
}The build script runs after its plugins, so it configures their suite (named) instead of registering one: a second suite of that name fails the build. A build that sets up its integration tests before the plugin applies, as a suite or a source set named integrationTest (from a convention plugin applied first, or from the root build), keeps them as they are, and the plugin registers nothing.
The tests start the containers and run the service on them:
private val postgres = PostgreSQLContainer("postgres:17-alpine").also { it.start() }
private val clickhouse = ClickHouseContainer(DockerImageName.parse(CLICKHOUSE_IMAGE)).also { it.start() }
private val server = wowServer<OrderingServer>(
"postgres",
"clickhouse",
"dev",
properties = mapOf(
"spring.datasource.url" to postgres.jdbcUrl,
"spring.datasource.username" to postgres.username,
"spring.datasource.password" to postgres.password,
"wow.clickhouse.endpoint" to clickhouse.httpUrl,
"wow.clickhouse.username" to clickhouse.username,
"wow.clickhouse.password" to clickhouse.password,
"wow.log.partitions" to 4,
),
)- The application's
postgresprofile defines itsDataSource, so the host chooses PostgreSQL; theclickhouseprofile turns the warehouse on. - The stores create their schemas when they start (
wow.ddl: auto), so a fresh container needs nothing else. wow.log.partitions=4keeps a small log: a test does not need 64 partitions.
Check the contract
The Gradle plugin me.ahoo.wow.service merges the service's Manifest from its modules and compares it with the committed baseline, manifest/baseline.json, in checkWowManifest, part of check. A change that breaks a client within the baseline's compatibility line, the service's major version, fails the build, listing each breaking change and its path, and so does a change the baseline does not record yet, or a missing baseline. After an intended change, updateWowManifestBaseline records the new baseline, with the line instead of the version; it refuses a break within the line. Commit it with the change, so its review shows what the clients will see. The Manifest explains what counts as breaking.
Test across services
Services that call each other are tested together the same way: start each in the test's JVM, point one at the other's internal port, and drive the flow. The eShop's :eshop-e2e module runs two services at a time so; its suite of all five runs each service in a JVM of its own, on PostgreSQL, Kafka and ClickHouse in containers.