There's a better option in newer versions of WireMock than calling addMockServiceRequestListener
, which is to register a PostServeAction
implementation as an extension when configuring JUnit:
@RegisterExtension
static WireMockExtension wm =
WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort()
.extensions(new PostServeAction() {
@Override
public String getName() {
return "my-action";
}
@Override
public void doGlobalAction(ServeEvent serveEvent, Admin admin) {
// Do something
}
}))
.build();
PostServeAction
implementations are the "proper" way to listen for events and will still work in future versions, whereas listeners will be deprecated and removed eventually. They also are given more context about the request than listeners.