Skip to content

Stub Metadata

It is possible to attach arbitrary metadata to stub mappings, which can be later used to search or deletion, or simply retrieval.

Data under the metadata key is a JSON object (represented in Java by a Map<String, ?>). It can be added to a stub mapping on creation.

stubFor(get("/with-metadata")
.withMetadata(metadata()
.attr("singleItem", 1234)
.list("listItem", 1, 2, 3, 4)
.attr("nestedObject", metadata()
.attr("innerItem", "Hello")
)
));

Stubs can be found by matching against their metadata using the same matching strategies as when matching HTTP requests. The most useful matcher for this is matchesJsonPath:

List<StubMapping> stubs =
findStubsByMetadata(matchingJsonPath("$.singleItem", containing("123")));

Similarly, stubs with matching metadata can be removed:

removeStubsByMetadata(matchingJsonPath("$.singleItem", containing("123")));

See Removing items from the journal