Skip to content

Commit

Permalink
docs: add message provider with metadata example to junit README
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 15, 2020
1 parent a0f51c0 commit bc51f43
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions provider/junit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ no parameters or a single Map parameter.
}
```

### Example of Message test that verifies metadata

To have the message metadata - such as the topic - also verified you need to return a `MessageAndMetadata` from the invoked method that contains the payload and metadata to be validation. For example, to verify the metadata of an integration using the Spring [Message](https://docs.spring.io/spring-integration/reference/html/message.html) interface, you can do something like the following:

```java
...

@PactVerifyProvider("a product event update")
public MessageAndMetadata verifyMessageForOrder() {
ProductEvent product = new ProductEvent("id1", "product name", "product type", "v1", EventType.CREATED);
Message<String> message = new ProductMessageBuilder().withProduct(product).build();

return generateMessageAndMetadata(message);
}

private MessageAndMetadata generateMessageAndMetadata(Message<String> message) {
HashMap<String, Object> metadata = new HashMap<String, Object>();
message.getHeaders().forEach((k, v) -> metadata.put(k, v));

return new MessageAndMetadata(message.getPayload().getBytes(), metadata);
}
```

_NOTE: this requires you to add medadata expections in your consumer test_

## Provider state callback methods

For the provider states in the pact being verified, you can define methods to be invoked to setup the correct state
Expand Down

0 comments on commit bc51f43

Please sign in to comment.