Skip to content

Support for scenarios to integration test application modules #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
odrotbohm opened this issue Feb 20, 2023 · 3 comments
Closed

Support for scenarios to integration test application modules #136

odrotbohm opened this issue Feb 20, 2023 · 3 comments
Assignees
Labels
in: test support Spring Boot integration testing type: enhancement Major enhanvements, new features
Milestone

Comments

@odrotbohm
Copy link
Member

Working with application modules, and ones driven by @ApplicationEventListeners in particular introduces a couple of challenges:

  • Events stimulating the module need to be published in a transactional way. Thus, the test code is likely to require access to TransactionOperations and ApplicationEventPublisher.
  • As these events are usually handled asynchronously, an Awaitility pipeline will have to be set up.
  • Success or failure of an operation is usually determined by another event being published or some state change on the module being observed.
  • Additional verifications on the resulting event, the observed state changed, or stimulus result need to be performed

While all of this can be assembled using already existing abstractions, it would be nice if there was a way to formulate such scenarios in a readable API. An event-based scenario could look something like this:

scenario.publish(new ProductAdded(productId)) // <1>
  .andWaitAtMost(Duration.ofSeconds(2)) // <2>
  .forEventOfType(InventoryItemAdded.class) // <3>
  .toArriveAndVerify(it -> { // <4>
    assertThat(inventory.findByProductIdentifier(productId)).isPresent();
    assertThat(inventory.findById(it.id())).isPresent();
  });

<1> Stimulates the system through an event publication.
<2> (Optional) Customizations of the execution
<3> Define expectation by describing the event that's supposed to appear, optionally defining filters.
<4> (Optional) Formulate additional verifications. The most simple variant is toArrive().

Alternatively, the scenario could be described based on state changes.

scenario.stimulate(tx -> …) // <1>
  .forStateChange(() -> inventory.findByProductIdentifier(productId)) // <2>
  .andVerify(it -> { // <3>
    assertThat(it).…;
  });

<1> Would trigger some method of a module's service or the like. Access TransactionOperations to make sure that transactional event listeners are triggered for events potentially published during the operation.
<2> Inspect some state on the module. By default, accept a non-null (non-empty in the case of an Optional) as concluding.
<3> Additional verifications based on the value returned by the stimulus or expected changed state (out of <2>).

@odrotbohm odrotbohm self-assigned this Feb 20, 2023
@odrotbohm odrotbohm added in: test support Spring Boot integration testing type: enhancement Major enhanvements, new features labels Feb 20, 2023
@odrotbohm odrotbohm added this to the 0.4 milestone Feb 20, 2023
odrotbohm added a commit that referenced this issue Feb 20, 2023
odrotbohm added a commit that referenced this issue Feb 20, 2023
@odrotbohm
Copy link
Member Author

Find a bit of preliminary documentation here.

odrotbohm added a commit that referenced this issue Feb 23, 2023
Move the registration of cleanup callbacks to the intermediate When type to avoid overloads of Scenario.stimulate(…). Also, to prevent multiple lambda-style parameters for ….stimulate(…) as they don't distinguish nice on the declaration side. Removed the varargs from ….publish(…) methods and wait for the actual request to add support for preparing, in transaction callbacks (potentially to be introduced as ….prepare(…) method).

The default acceptance criteria for state change expectations now also considers a boolean true as concluding method result.
odrotbohm added a commit that referenced this issue Feb 23, 2023
@danstooamerican
Copy link

This looks very promising, can't wait to try it!

Is it already included in a snapshot version?

@odrotbohm
Copy link
Member Author

Available in 0.4 / 0.4.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test support Spring Boot integration testing type: enhancement Major enhanvements, new features
Projects
None yet
Development

No branches or pull requests

2 participants