The Itinera bookJava edition

Part 4: Running it for real, Chapter 30

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:

Java, from the program ch30_testing_a_whole_journey.test
/** A bank for tests: it charges every card, or its network is down. */
enum TestBank implements Payments {
  CHARGES,
  IS_DOWN;

  @Override
  public Optional<String> charge(String order, String card, long amount) throws NetworkDown {
    return switch (this) {
      case CHARGES -> Optional.of("R-" + order);
      case IS_DOWN -> throw new NetworkDown();
    };
  }

  @Override
  public Optional<String> replacement(String card) {
    return Optional.empty();
  }
}

/** A carrier for tests: it books every parcel, and collects every parcel or none. */
enum TestCarrier implements Carrier {
  COLLECTS_ALL,
  COLLECTS_NONE;

  @Override
  public String book(String order, long weight) {
    return "T-" + order;
  }

  @Override
  public boolean collects(long weight) {
    return 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:

  1. fakeProvider names one of the workflow's providers, bank, from chapter 7, or carriers, 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 lambda the executor calls in the provider's place. The lambda returns what a provider's provide(...) returns, a Provided.Instance, from org.itineradev.itinera.service, holding the test's fake.
  2. reporter adds a reporter to this instance's journey alone, after the workflow's own. Here it adds a RecordingReporter, from org.itineradev.itinera.report.testing, which keeps every event, and whose getPath() gives the journey's path.
Java, from the program ch30_testing_a_whole_journey.test
/**
 * 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 static List<String> pathOf(String order, long weight, TestBank bank, TestCarrier carrier)
    throws Refusal {
  var recorder = new RecordingReporter();
  var parcel = new Parcel(weight, List.of(20L, 15L, 5L));
  var instance =
      TestInstanceBuilder.of(new Orders("email"))
          .initialData(new NewOrder(order, "Ann", 30, parcel, "ann-2028"))
          .reporter(recorder)
          .fakeProvider("bank", Payments.class, () -> new Provided.Instance<>(bank))
          .fakeProvider("carriers", Carrier.class, () -> new Provided.Instance<>(carrier))
          .create();
  new LocalExecutor().run(instance);
  return recorder.getPath();
}

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:

Java, from the program ch30_testing_a_whole_journey.test
@Test
void an_order_the_bank_charges_and_the_carrier_takes_is_shipped() throws Refusal {
  assertEquals(
      List.of(
          "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
void a_parcel_over_the_limit_is_given_up_after_its_first_attempt() throws Refusal {
  assertEquals(
      List.of(
          "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
void a_charge_whose_bank_is_down_is_given_up_and_never_rescued() throws Refusal {
  assertEquals(
      List.of(
          "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
void a_parcel_no_carrier_collects_is_tried_once_more_then_given_up() throws Refusal {
  assertEquals(
      List.of(
          "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));
}

The console launcher prints:

What running the tests of ch30_testing_a_whole_journey prints, recorded in ch30_testing_a_whole_journey.txt
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 303 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

  1. A fake provider replaces only the provider's method. The workflow keeps its providers' keys and scopes. The steps still ask for payments and carrier, and carriers stays a provider called for every attempt. Only what the provider returns changes, and only for the journey of the instance its builder creates.
  2. 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.
  3. A path is the journey's flow, one line for each event of it. getPath() reads 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.
  4. Each line is a plain sentence. charge attempt 1 terminated abnormally names the step, its attempt and how it ended, with the reason or the error after a colon. ship given up: FailWorkflow from failedShipping names the cause, and the policy that decided it. journey failed at ship: no-carrier-can-take-it names the step where the journey ended, and the reason it ended with: after retries are exhausted, the last attempt's reason, too-heavy for the parcel no carrier collects.
  5. 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.