Testing a hook or an adapter alone
What you want to do
Some of the shop's rules live outside its steps: failedShipping deciding that no retry can help and refunding the charge, the audit line, the customer's message, and the adapter taking the weight out of the parcel. You want to check each of them on its own, as chapter 27 checked the steps.
The code
The program is chapter 25's shop, with its tests in a file beside it, as in chapter 27. The notify policy tells customers through the role Messages, shown in chapter 19, and failedShipping gives money back through the role Refunds, shown in chapter 23, so the tests bring messages and refunds of their own, which keep what they are asked to do in a list:
/** Messages for tests: it keeps each message it is asked to send, with the customer's name. */
class TestMessages : Messages {
val told = mutableListOf<String>()
override fun tell(customer: String, message: String) {
told += "$customer: $message"
}
}
/** Refunds for tests: it keeps each refund it is asked to give, with its receipt. */
class TestRefunds : Refunds {
val given = mutableListOf<String>()
override fun refund(receipt: String, amount: Long) {
given += "$amount back on $receipt"
}
}
/** Messages for tests: it keeps each message it is asked to send, with the customer's name. */
class TestMessages : Messages {
val told = mutableListOf<String>()
override suspend fun tell(customer: String, message: String) {
told += "$customer: $message"
}
}
/** Refunds for tests: it keeps each refund it is asked to give, with its receipt. */
class TestRefunds : Refunds {
val given = mutableListOf<String>()
override suspend fun refund(receipt: String, amount: Long) {
given += "$amount back on $receipt"
}
}
The policies and the adapter are the shop's own, unchanged since they were shown: the audit in chapter 20, Notify in chapter 19, FailedShipping in chapter 23, and the adapter of chapter 21, a method of the workflow class as chapter 23 shows it, and Parcel, which chapter 21 also shows. This chapter shows only their tests. FailedShipping's hook beforeRetrying is called for Ned's shipping, with the reason each test chooses, and the tests read the decision it returns and the refunds it asked for. Its hook givenUp is called with the cause RETRIES_EXHAUSTED, as for Oli's shipping, and the test also reads the refund it contributed:
/**
* What `beforeRetrying` decides for Ned's shipping, which failed with this reason after he paid
* 50, and the refunds it asked for.
*/
private fun beforeRetrying(reason: Reason?): Pair<OnFailure, List<String>> {
val refunds = TestRefunds()
val contributions = Contributions()
val decision =
FailedShipping()
.beforeRetrying(reason, "R-A-12", 50, refunds, contributions.contributor)
return decision to refunds.given
}
@Test
fun `failedShipping refunds and gives up a shipping that failed with over-parcel-limit`() {
val (decision, refunds) = beforeRetrying(Reason("over-parcel-limit"))
assertEquals("no-carrier-can-take-it", decision.failsWorkflow?.code)
assertEquals(listOf("50 back on R-A-12"), refunds)
}
@Test
fun `failedShipping lets a shipping that failed with too-heavy be retried`() {
val (decision, refunds) = beforeRetrying(Reason("too-heavy"))
assertTrue(decision.keepsDefault)
assertEquals(emptyList<String>(), refunds)
}
@Test
fun `failedShipping lets a shipping that ended in an error be retried`() {
val (decision, refunds) = beforeRetrying(null)
assertTrue(decision.keepsDefault)
assertEquals(emptyList<String>(), refunds)
}
@Test
fun `failedShipping refunds a shipping the executor gave up`() {
val refunds = TestRefunds()
val contributions = Contributions()
val decision =
FailedShipping()
.givenUp(
FailureCause.RETRIES_EXHAUSTED,
"R-A-14",
20,
refunds,
contributions.contributor,
)
assertTrue(decision.keepsDefault)
assertEquals(listOf("20 back on R-A-14"), refunds.given)
assertEquals(
"20 back on R-A-14: retries exhausted",
contributions.read("refund", typeToken<String>()).orNull(),
)
}
/**
* What `beforeRetrying` decides for Ned's shipping, which failed with this reason after he paid
* 50, and the refunds it asked for.
*/
private suspend fun beforeRetrying(reason: Reason?): Pair<OnFailure, List<String>> {
val refunds = TestRefunds()
val contributions = Contributions()
val decision =
FailedShipping()
.beforeRetrying(reason, "R-A-12", 50, refunds, contributions.contributor)
return decision to refunds.given
}
@Test
fun `failedShipping refunds and gives up a shipping that failed with over-parcel-limit`() =
runBlocking<Unit> {
val (decision, refunds) = beforeRetrying(Reason("over-parcel-limit"))
assertEquals("no-carrier-can-take-it", decision.failsWorkflow?.code)
assertEquals(listOf("50 back on R-A-12"), refunds)
}
@Test
fun `failedShipping lets a shipping that failed with too-heavy be retried`() =
runBlocking<Unit> {
val (decision, refunds) = beforeRetrying(Reason("too-heavy"))
assertTrue(decision.keepsDefault)
assertEquals(emptyList<String>(), refunds)
}
@Test
fun `failedShipping lets a shipping that ended in an error be retried`() =
runBlocking<Unit> {
val (decision, refunds) = beforeRetrying(null)
assertTrue(decision.keepsDefault)
assertEquals(emptyList<String>(), refunds)
}
@Test
fun `failedShipping refunds a shipping the executor gave up`() =
runBlocking<Unit> {
val refunds = TestRefunds()
val contributions = Contributions()
val decision =
FailedShipping()
.givenUp(
FailureCause.RETRIES_EXHAUSTED,
"R-A-14",
20,
refunds,
contributions.contributor,
)
assertTrue(decision.keepsDefault)
assertEquals(listOf("20 back on R-A-14"), refunds.given)
assertEquals(
"20 back on R-A-14: retries exhausted",
contributions.read("refund", typeToken<String>()).orNull(),
)
}
The audit's hook is called with the step's name, made by StepName.of, from org.itineradev.itinera.step, the amount, the receipt, and a contributor lent by Contributions:
@Test
fun `audit writes the step, the amount and the receipt`() {
val contributions = Contributions()
Audit().charged(StepName.of("charge"), 30, "R-A-1", contributions.contributor)
assertEquals(
"charge took 30, receipt R-A-1",
contributions.read("audit", typeToken<String>()).orNull(),
)
}
@Test
fun `audit writes the step, the amount and the receipt`() =
runBlocking<Unit> {
val contributions = Contributions()
Audit().charged(StepName.of("charge"), 30, "R-A-1", contributions.contributor)
assertEquals(
"charge took 30, receipt R-A-1",
contributions.read("audit", typeToken<String>()).orNull(),
)
}
Both hooks of Notify are called with the test's messages, for Kim's gift card and Lee's failed order:
@Test
fun `notify gives a gift card's code and says sorry for a failed order`() {
val messages = TestMessages()
Notify().succeeded("Kim", "A-9", tracking = null, code = "G-A-9", messages)
Notify().failed("Lee", "A-10", messages)
assertEquals(
listOf(
"Kim: Order A-9 is paid: your gift card's code is G-A-9.",
"Lee: Sorry, order A-10 could not be completed.",
),
messages.told,
)
}
@Test
fun `notify gives a gift card's code and says sorry for a failed order`() =
runBlocking<Unit> {
val messages = TestMessages()
Notify().succeeded("Kim", "A-9", tracking = null, code = "G-A-9", messages)
Notify().failed("Lee", "A-10", messages)
assertEquals(
listOf(
"Kim: Order A-9 is paid: your gift card's code is G-A-9.",
"Lee: Sorry, order A-10 could not be completed.",
),
messages.told,
)
}
The adapter is a method of the workflow class, so the tests make the workflow, with its channel, and ask it for two of the shipping's inputs:
/** Gil's parcel, as the warehouse describes it. */
private fun gilsParcel(): Parcel = Parcel(weight = 60, size = listOf(120, 80, 60))
@Test
fun `parcels gives the shipping the parcel's weight`() {
val weight = Orders(channel = "email").parcels("weight", gilsParcel())
assertEquals(Supplied.Value(60L), weight)
}
@Test
fun `parcels leaves every other input to the data bag`() {
val receipt = Orders(channel = "email").parcels("receipt", gilsParcel())
assertEquals(Supplied.FromDataBag, receipt)
}/** Gil's parcel, as the warehouse describes it. */
private fun gilsParcel(): Parcel = Parcel(weight = 60, size = listOf(120, 80, 60))
@Test
fun `parcels gives the shipping the parcel's weight`() =
runBlocking<Unit> {
val weight = Orders(channel = "email").parcels("weight", gilsParcel())
assertEquals(Supplied.Value(60L), weight)
}
@Test
fun `parcels leaves every other input to the data bag`() =
runBlocking<Unit> {
val receipt = Orders(channel = "email").parcels("receipt", gilsParcel())
assertEquals(Supplied.FromDataBag, receipt)
}In the asynchronous form, which tests chapter 26's shop, the fakes' methods are suspend functions, as the roles' are there, and each test runs its body in runBlocking, which waits for the hook or the adapter it calls.
JUnit's console launcher prints the same for both forms:
JUnit Jupiter > HooksTest > audit writes the step, the amount and the receipt() :: STARTED
JUnit Jupiter > HooksTest > audit writes the step, the amount and the receipt() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping lets a shipping that ended in an error be retried() :: STARTED
JUnit Jupiter > HooksTest > failedShipping lets a shipping that ended in an error be retried() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping lets a shipping that failed with too-heavy be retried() :: STARTED
JUnit Jupiter > HooksTest > failedShipping lets a shipping that failed with too-heavy be retried() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping refunds a shipping the executor gave up() :: STARTED
JUnit Jupiter > HooksTest > failedShipping refunds a shipping the executor gave up() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping refunds and gives up a shipping that failed with over-parcel-limit() :: STARTED
JUnit Jupiter > HooksTest > failedShipping refunds and gives up a shipping that failed with over-parcel-limit() :: SUCCESSFUL
JUnit Jupiter > HooksTest > notify gives a gift card's code and says sorry for a failed order() :: STARTED
JUnit Jupiter > HooksTest > notify gives a gift card's code and says sorry for a failed order() :: SUCCESSFUL
JUnit Jupiter > HooksTest > parcels gives the shipping the parcel's weight() :: STARTED
JUnit Jupiter > HooksTest > parcels gives the shipping the parcel's weight() :: SUCCESSFUL
JUnit Jupiter > HooksTest > parcels leaves every other input to the data bag() :: STARTED
JUnit Jupiter > HooksTest > parcels leaves every other input to the data bag() :: SUCCESSFUL
Test run finished after 305 ms
[ 2 containers found ]
[ 0 containers skipped ]
[ 2 containers started ]
[ 0 containers aborted ]
[ 2 containers successful ]
[ 0 containers failed ]
[ 8 tests found ]
[ 0 tests skipped ]
[ 8 tests started ]
[ 0 tests aborted ]
[ 8 tests successful ]
[ 0 tests failed ]JUnit Jupiter > HooksTest > audit writes the step, the amount and the receipt() :: STARTED
JUnit Jupiter > HooksTest > audit writes the step, the amount and the receipt() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping lets a shipping that ended in an error be retried() :: STARTED
JUnit Jupiter > HooksTest > failedShipping lets a shipping that ended in an error be retried() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping lets a shipping that failed with too-heavy be retried() :: STARTED
JUnit Jupiter > HooksTest > failedShipping lets a shipping that failed with too-heavy be retried() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping refunds a shipping the executor gave up() :: STARTED
JUnit Jupiter > HooksTest > failedShipping refunds a shipping the executor gave up() :: SUCCESSFUL
JUnit Jupiter > HooksTest > failedShipping refunds and gives up a shipping that failed with over-parcel-limit() :: STARTED
JUnit Jupiter > HooksTest > failedShipping refunds and gives up a shipping that failed with over-parcel-limit() :: SUCCESSFUL
JUnit Jupiter > HooksTest > notify gives a gift card's code and says sorry for a failed order() :: STARTED
JUnit Jupiter > HooksTest > notify gives a gift card's code and says sorry for a failed order() :: SUCCESSFUL
JUnit Jupiter > HooksTest > parcels gives the shipping the parcel's weight() :: STARTED
JUnit Jupiter > HooksTest > parcels gives the shipping the parcel's weight() :: SUCCESSFUL
JUnit Jupiter > HooksTest > parcels leaves every other input to the data bag() :: STARTED
JUnit Jupiter > HooksTest > parcels leaves every other input to the data bag() :: SUCCESSFUL
Test run finished after 381 ms
[ 2 containers found ]
[ 0 containers skipped ]
[ 2 containers started ]
[ 0 containers aborted ]
[ 2 containers successful ]
[ 0 containers failed ]
[ 8 tests found ]
[ 0 tests skipped ]
[ 8 tests started ]
[ 0 tests aborted ]
[ 8 tests successful ]
[ 0 tests failed ]How it works
- A hook is a method. The test makes the policy with its constructor, which takes nothing, and calls the hook with what its parameters ask for. Their types and annotations, such as
Reason?,@FromWorkflowor@Role, only tell the executor what to give them in a journey. Nothing else changes: the policies are the shop's own. - A hook is tested through its decision.
beforeRetryingreturns anOnFailure, andgivenUpone too.OnFailure's own properties read it:keepsDefaultsays the hook kept the default, so the executor does what it would have done without the policy, andfailsWorkflowgives the reason the hook fails the journey with, ornullwhen it doesn't. The tests give the hook every case it has to handle, including no reason at all, which is what it gets after an attempt that ended in an error. - A role is faked like a service.
Notifyasks forMessages, andFailedShippingforRefunds, so the tests pass their ownTestMessagesandTestRefunds, and read what the hooks told whom and what they gave back.refund, the policy's own private method, is tested through the two hooks that call it. - A hook's contributions are read as a step's. The audit and
givenUpcontribute through the contributorContributionslends, and the tests read it back withread, as in chapter 27. - An adapter is tested through what it supplies.
parcelsreturns whatsupply(...)makes for the weight, aSupplied.Valueholding it, and whatfromDataBag()makes for every other input,Supplied.FromDataBag, which leaves the input to the data bag. Both are values the test compares, fromorg.itineradev.itinera.workflow. The test gives the adapter the parcel itself, as a journey would give it from the data bag.
So each rule of the shop's policies and its workflow is checked alone too. But nothing yet checks how the workflow puts them together: that a declined card is rescued, that Ned's shipping is given up after its first attempt, and that every customer is told. A journey checks that, and a journey runs the steps, with their bank and their carriers.
Outcomes
We have shown how to test a policy's hooks and a workflow's input adapter alone, by calling them as methods.
| The usual way | With Itinera |
|---|---|
| Logic that runs around a call, such as a retry rule or an audit line, is often tested only by running the whole call. | A hook is a method, called by the test with the data it asks for, and its decision is a value the test reads. |
| Code that converts data for a function often sits inside it, tested only with it. | An input adapter is a method of the workflow, tested on its own with the data it reads. |