Class OpenApiValidationListener

java.lang.Object
com.atlassian.oai.validator.wiremock.OpenApiValidationListener
All Implemented Interfaces:
com.github.tomakehurst.wiremock.http.RequestListener

public class OpenApiValidationListener extends Object implements com.github.tomakehurst.wiremock.http.RequestListener
A WireMock request listener that applies OpenAPI / Swagger validation to WireMock interactions.

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:
  • Constructor Details

    • OpenApiValidationListener

      public OpenApiValidationListener(String specUrlOrDefinition)
    • OpenApiValidationListener

      public OpenApiValidationListener(OpenApiInteractionValidator validator)
  • Method Details

    • requestReceived

      public void requestReceived(com.github.tomakehurst.wiremock.http.Request request, com.github.tomakehurst.wiremock.http.Response response)
      Specified by:
      requestReceived in interface com.github.tomakehurst.wiremock.http.RequestListener
    • getReport

      public ValidationReport getReport()
      Access the current validation report. This will contain all messages since the last call to reset().

      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.