Class OpenApiValidationListener
- All Implemented Interfaces:
com.github.tomakehurst.wiremock.http.RequestListener
The listener can be added to a WireMockRule or
WireMockServer instance
(see Extending WireMock). The current validation report
can be accessed with getReport(), and the convenience method assertValidationPassed() can be used
to check for validation errors and throw an exception if any are found.
Important: The listener will continue accumulating validation errors on each call to the WireMock server.
Call reset() before your test to ensure you only get validation errors for the current test execution.
E.g.
@Rule
public WireMockRule wireMockRule;
private OpenApiValidationListener validationListener;
public ValidatedWireMockTestExample() {
this.validationListener = new OpenApiValidationListener(SPEC_URL);
this.wireMockRule = new WireMockRule(PORT);
this.wireMockRule.addMockServiceRequestListener(validationListener);
}
@After
public void teardown() {
this.validationListener.reset();
}
@Test
public void testFoo() {
// Some interactions with the WireMock server
...
this.validationListener.assertValidationPassed();
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAssert that the current validation report contains no errors and fail if it does.Access the current validation report.voidrequestReceived(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response) voidreset()Reset this listener instance and remove validation messages from the validation report.
-
Constructor Details
-
OpenApiValidationListener
-
OpenApiValidationListener
-
-
Method Details
-
requestReceived
public void requestReceived(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response) - Specified by:
requestReceivedin interfacecom.github.tomakehurst.wiremock.http.RequestListener
-
getReport
Access the current validation report. This will contain all messages since the last call toreset().Most often clients will simply want to invoke
assertValidationPassed()rather than access the report directly.- Returns:
- the current validation report.
-
reset
public void reset()Reset this listener instance and remove validation messages from the validation report.This method should be invoked between tests to ensure validation messages don't carry over between test runs e.g.
@After public void tearDown() { validationListener.reset(); } -
assertValidationPassed
public void assertValidationPassed()Assert that the current validation report contains no errors and fail if it does.- Throws:
OpenApiValidationListener.OpenApiValidationException- if the current validation report contains any errors.
-