Testing a whole journey
What you want to do
You want to run one order through the shop's real steps and policies, from start to end, and check the way its journey went: which steps succeeded, which failed and why, and how the journey ended. The bank and the carriers should be fakes, so the test never reaches the outside world.
The code
The program is chapter 25's shop, with its tests in a file beside it, as in chapter 27, and the test class quiets what the program prints as chapter 29's does. The tests bring their own bank, which charges or is down, and their own carrier, which collects every parcel or none, implementing Payments and Carrier as chapter 14 and chapter 12 declared them, and throwing chapter 12's error:
/** A bank for tests: it charges every card, or its network is down. */
enum class TestBank : Payments {
CHARGES,
IS_DOWN;
override fun charge(order: String, card: String, amount: Long): String? =
when (this) {
CHARGES -> "R-$order"
IS_DOWN -> throw NetworkDown()
}
override fun replacement(card: String): String? = null
}
/** A carrier for tests: it books every parcel, and collects every parcel or none. */
enum class TestCarrier : Carrier {
COLLECTS_ALL,
COLLECTS_NONE;
override fun book(order: String, weight: Long): String = "T-$order"
override fun collects(weight: Long): Boolean = this == COLLECTS_ALL
}
/** A bank for tests: it charges every card, or its network is down. */
enum class TestBank : Payments {
CHARGES,
IS_DOWN;
override suspend fun charge(order: String, card: String, amount: Long): String? =
when (this) {
CHARGES -> "R-$order"
IS_DOWN -> throw NetworkDown()
}
override suspend fun replacement(card: String): String? = null
}
/** A carrier for tests: it books every parcel, and collects every parcel or none. */
enum class TestCarrier : Carrier {
COLLECTS_ALL,
COLLECTS_NONE;
override suspend fun book(order: String, weight: Long): String = "T-$order"
override suspend fun collects(weight: Long): Boolean = this == COLLECTS_ALL
}
Each test runs an order through pathOf, which makes an instance of the shop's workflow with TestInstanceBuilder.of(...), from org.itineradev.itinera.instance.testing, in place of instance(). It takes the same initial data as main's instances, and has two more calls:
fakeProvidernames one of the workflow's providers,bank, from chapter 7, orcarriers, from chapter 13, both methods of the workflow class as chapter 23 shows it. It takes the class of the service the provider makes, and a function the executor calls in the provider's place. The function returns what a provider'sprovide(...)returns, aProvided.Instance, fromorg.itineradev.itinera.service, holding the test's fake.reporteradds a reporter to this instance's journey alone, after the workflow's own. Here it adds aRecordingReporter, fromorg.itineradev.itinera.report.testing, which keeps every event, and whosepathgives the journey's path.
/**
* Runs an order of 30 with a parcel of this weight, through the shop's real steps and policies,
* with the test's bank and carriers in place of the shop's, and gives back its journey's path.
*/
private fun pathOf(
order: String,
weight: Long,
bank: TestBank,
carrier: TestCarrier,
): List<String> {
val recorder = RecordingReporter()
val instance =
TestInstanceBuilder.of(Orders(channel = "email"))
.initialData(
NewOrder(
order,
"Ann",
price = 30,
parcel = Parcel(weight, size = listOf(20, 15, 5)),
card = "ann-2028",
)
)
.reporter(recorder)
.fakeProvider("bank", Payments::class.java) { Provided.Instance(bank) }
.fakeProvider("carriers", Carrier::class.java) { Provided.Instance(carrier) }
.create()
LocalExecutor().run(instance)
return recorder.path
}
/**
* Runs an order of 30 with a parcel of this weight, through the shop's real steps and policies,
* with the test's bank and carriers in place of the shop's, and gives back its journey's path.
*/
private suspend fun pathOf(
order: String,
weight: Long,
bank: TestBank,
carrier: TestCarrier,
): List<String> {
val recorder = AsyncRecordingReporter()
val instance =
AsyncTestInstanceBuilder.of(Orders(channel = "email"))
.initialData(
NewOrder(
order,
"Ann",
price = 30,
parcel = Parcel(weight, size = listOf(20, 15, 5)),
card = "ann-2028",
)
)
.reporter(recorder)
.fakeProvider("bank", Payments::class.java) { Provided.Instance(bank) }
.fakeProvider("carriers", Carrier::class.java) { Provided.Instance(carrier) }
.create()
AsyncLocalExecutor().run(instance)
return recorder.path
}
NewOrder is as chapter 25 shows it, and Parcel as chapter 21 does.
The shop approves the path of each kind of order once, and writes it down. The paths follow the workflow as chapter 23 declares it, with its steps, policies and reporters where that page says they are shown. Each test compares a journey's path with the approved one:
@Test
fun `an order the bank charges and the carrier takes is shipped`() {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 succeeded",
"journey succeeded",
),
pathOf("A-1", 2, TestBank.CHARGES, TestCarrier.COLLECTS_ALL),
)
}
@Test
fun `a parcel over the limit is given up after its first attempt`() {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 failed: over-parcel-limit",
"ship given up: FailWorkflow from failedShipping",
"journey failed at ship: no-carrier-can-take-it",
),
pathOf("A-2", 90, TestBank.CHARGES, TestCarrier.COLLECTS_ALL),
)
}
@Test
fun `a charge whose bank is down is given up and never rescued`() {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 terminated abnormally: the payment network is down",
"charge given up: FailWorkflow from outage",
"journey failed at charge: payment-network-down",
),
pathOf("A-3", 2, TestBank.IS_DOWN, TestCarrier.COLLECTS_ALL),
)
}
@Test
fun `a parcel no carrier collects is tried once more, then given up`() {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 failed: too-heavy",
"ship retrying",
"ship attempt 2 failed: too-heavy",
"ship given up: retries exhausted",
"journey failed at ship: too-heavy",
),
pathOf("A-4", 60, TestBank.CHARGES, TestCarrier.COLLECTS_NONE),
)
}@Test
fun `an order the bank charges and the carrier takes is shipped`() =
runBlocking<Unit> {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 succeeded",
"journey succeeded",
),
pathOf("A-1", 2, TestBank.CHARGES, TestCarrier.COLLECTS_ALL),
)
}
@Test
fun `a parcel over the limit is given up after its first attempt`() =
runBlocking<Unit> {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 failed: over-parcel-limit",
"ship given up: FailWorkflow from failedShipping",
"journey failed at ship: no-carrier-can-take-it",
),
pathOf("A-2", 90, TestBank.CHARGES, TestCarrier.COLLECTS_ALL),
)
}
@Test
fun `a charge whose bank is down is given up and never rescued`() =
runBlocking<Unit> {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 terminated abnormally: the payment network is down",
"charge given up: FailWorkflow from outage",
"journey failed at charge: payment-network-down",
),
pathOf("A-3", 2, TestBank.IS_DOWN, TestCarrier.COLLECTS_ALL),
)
}
@Test
fun `a parcel no carrier collects is tried once more, then given up`() =
runBlocking<Unit> {
assertEquals(
listOf(
"lookup attempt 1 succeeded",
"price attempt 1 succeeded",
"charge attempt 1 succeeded",
"ship attempt 1 failed: too-heavy",
"ship retrying",
"ship attempt 2 failed: too-heavy",
"ship given up: retries exhausted",
"journey failed at ship: too-heavy",
),
pathOf("A-4", 60, TestBank.CHARGES, TestCarrier.COLLECTS_NONE),
)
}In the asynchronous form, which tests chapter 26's shop, the fakes' methods are suspend functions, pathOf makes the instance with AsyncTestInstanceBuilder.of(...), records its events with an AsyncRecordingReporter, and runs it on an AsyncLocalExecutor, and each test runs its body in runBlocking.
The console launcher prints the same for both forms:
JUnit Jupiter > JourneysTest > a charge whose bank is down is given up and never rescued() :: STARTED
JUnit Jupiter > JourneysTest > a charge whose bank is down is given up and never rescued() :: SUCCESSFUL
JUnit Jupiter > JourneysTest > a parcel no carrier collects is tried once more, then given up() :: STARTED
JUnit Jupiter > JourneysTest > a parcel no carrier collects is tried once more, then given up() :: SUCCESSFUL
JUnit Jupiter > JourneysTest > a parcel over the limit is given up after its first attempt() :: STARTED
JUnit Jupiter > JourneysTest > a parcel over the limit is given up after its first attempt() :: SUCCESSFUL
JUnit Jupiter > JourneysTest > an order the bank charges and the carrier takes is shipped() :: STARTED
JUnit Jupiter > JourneysTest > an order the bank charges and the carrier takes is shipped() :: SUCCESSFUL
Test run finished after 788 ms
[ 2 containers found ]
[ 0 containers skipped ]
[ 2 containers started ]
[ 0 containers aborted ]
[ 2 containers successful ]
[ 0 containers failed ]
[ 4 tests found ]
[ 0 tests skipped ]
[ 4 tests started ]
[ 0 tests aborted ]
[ 4 tests successful ]
[ 0 tests failed ]JUnit Jupiter > JourneysTest > a charge whose bank is down is given up and never rescued() :: STARTED
JUnit Jupiter > JourneysTest > a charge whose bank is down is given up and never rescued() :: SUCCESSFUL
JUnit Jupiter > JourneysTest > a parcel no carrier collects is tried once more, then given up() :: STARTED
JUnit Jupiter > JourneysTest > a parcel no carrier collects is tried once more, then given up() :: SUCCESSFUL
JUnit Jupiter > JourneysTest > a parcel over the limit is given up after its first attempt() :: STARTED
JUnit Jupiter > JourneysTest > a parcel over the limit is given up after its first attempt() :: SUCCESSFUL
JUnit Jupiter > JourneysTest > an order the bank charges and the carrier takes is shipped() :: STARTED
JUnit Jupiter > JourneysTest > an order the bank charges and the carrier takes is shipped() :: SUCCESSFUL
Test run finished after 759 ms
[ 2 containers found ]
[ 0 containers skipped ]
[ 2 containers started ]
[ 0 containers aborted ]
[ 2 containers successful ]
[ 0 containers failed ]
[ 4 tests found ]
[ 0 tests skipped ]
[ 4 tests started ]
[ 0 tests aborted ]
[ 4 tests successful ]
[ 0 tests failed ]How it works
- A fake provider replaces only the provider's function. The workflow keeps its providers' keys and scopes. The steps still ask for
paymentsandcarrier, andcarriersstays a provider called for every attempt. Only what the provider returns changes, and only for the journey of the instance its builder creates. - Everything else is real. The steps run their own rules: the shipping refuses the parcel of 90 kg itself, with
over-parcel-limit, as chapter 27's test showed. The policies decide, the adapter supplies the weight, and the result and the events are the ones a real order would give. - A path is the journey's flow, one line for each event of it.
pathreads only the events that say how the flow went: each attempt's outcome, each decision about a step, and how the journey ended. Everything else, such as which provider served a step or which data was committed, is left out, so a path changes only when the flow does. - Each line is a plain sentence.
charge attempt 1 terminated abnormallynames the step, its attempt and how it ended, with the reason or the error after a colon.ship given up: FailWorkflow from failedShippingnames the cause, and the policy that decided it.journey failed at ship: no-carrier-can-take-itnames the step where the journey ended, and the reason it ended with: after retries are exhausted, the last attempt's reason,too-heavyfor the parcel no carrier collects. - An approved path is a decision of the flow, written down. A person reads it once and agrees that this is what should happen to such an order. If a change to the workflow sends an order another way, the test fails. A change to a step's rules that keeps the flow keeps the path.
So the shop tests each piece alone, its flow with scripts, and whole journeys by their paths. But the shop's log will soon write to a logging service, over the network too, where a write can fail.
Outcomes
We have shown how to test a whole journey, with fake providers in place of the workflow's, by comparing its path with an approved one.
| The usual way | With Itinera |
|---|---|
| Testing a whole request often needs a test double for every dependency, wired by hand into the code under test. | fakeProvider replaces a workflow's provider by name, and the steps get the fake as they would the real service. |
| An end-to-end test usually checks the final state, and says little about the way a request went. | A journey's path lists each outcome and decision in order, and a test compares it with the path the shop approved. |