Triggers
When writing a contract for an HTTP client, you will need to define a trigger
function for each interaction.
In it, you should call your real client code
here, and return the business object that your client returns.
Interactions where the client code succeeds
If your interaction models a success (ie, it doesn't throw any exceptions or reject any promises) then:
- ContractCase calls your
trigger
function to invoke the client code for that interaction. - If there was no exception, it passes the object returned from the trigger to your
testResponse
function - If there was an exception from the
trigger
, the test is marked as failed, and the contract definition will not be successful.
Interactions where the client code is expected to fail
If your interaction models a failure (ie, it doesn't return an object, and instead throws an exception / rejects a promise / completes a future exceptionally), then:
- ContractCase calls your
trigger
function to invoke the client code for that interaction. - If there was no exception, the test is marked as failed, and the contract definition will not be successful.
- If there was an exception from the
trigger
, that exception is passed to yourtestErrorResponse
function
Writing a trigger
Triggers are functions with one InteractionSetup
argument. The interaction setup
object contains the details of the mock that ContractCase has set up,
and the resolved values for any state variables.
In an HTTP client test, this will include the base URL for the mock server:
- Typescript
- Java
trigger: (interactionSetup: HttpRequestConfig) =>
new YourApi(interactionSetup.mock.baseUrl).getUser(
interactionSetup.getStateVariable('userId'),
),
IndividualSuccessTestConfigBuilder.builder()
.withTrigger(
(interactionSetup) ->
new YourApiClient(
interactionSetup.getMockSetup("baseUrl")
).getUserQuery(
interactionSetup.getStateVariable("userId")
)
)