Skip to content

Latest commit

 

History

History
65 lines (53 loc) · 2.32 KB

File metadata and controls

65 lines (53 loc) · 2.32 KB

OpenAPI Request Validator - WireMock - JUnit5

maven-central

WireMock extension OpenApiValidationListener that applies OpenAPI / Swagger validation to WireMock requests and responses. This extension is compatible with JUnit5+.

Usage

OpenApiValidationListener implements the WireMock ServeEventListener interface and is the recommended extension for new projects. It performs validation after the full request/response cycle has completed.

  1. Add dependency to your project.
<dependency>
    <groupId>com.atlassian.oai</groupId>
    <artifactId>openapi-request-validator-wiremock-junit5</artifactId>
    <version>${openapi-request-validator.version}</version>
</dependency>
  1. Add the WireMock extension OpenApiValidationListener to your WireMock instance. See examples below.
  2. Use getReport() and/or the convenience method assertValidationPassed() to check the validations.

Example with global validation (one OpenAPI spec URL for all):

private static final OpenApiValidationListener VALIDATION_LISTENER = new OpenApiValidationListener(SPEC_URL);

@RegisterExtension
private static final WireMockExtension WIREMOCK = WireMockExtension.newInstance()
    .options(wireMockConfig()
        .dynamicPort()
        .extensions(VALIDATION_LISTENER))
    .build();

@AfterEach
void teardown() {
    VALIDATION_LISTENER.reset();
}

@Test
void testFoo() {
    // Some interactions with the WireMock server
    VALIDATION_LISTENER.assertValidationPassed();
}

Example with local validation (OpenAPI spec URL per stub mapping):

private static final OpenApiValidationListener VALIDATION_LISTENER = new OpenApiValidationListener();

@RegisterExtension
private static final WireMockExtension WIREMOCK = WireMockExtension.newInstance()
    .options(wireMockConfig()
        .dynamicPort()
        .extensions(VALIDATION_LISTENER))
    .build();

@BeforeEach
void setUp() {
    WIREMOCK.stubFor(get(urlPathMatching("/test"))
        .withServeEventListener("open-api-validation-listener",
            new OpenApiValidationListener.OasUrlParameter(SPEC_URL))
        .willReturn(ok()));
}