1

Well, I am stuck in this scenario that I want to use Swagger API to validate response of my Lagom Service API. Here is some sample code:

@Test
public void shouldPayloadFromFileConformToSchema() throws Exception {
    // first test the plain json is valid with schema
    SwaggerRequestResponseValidator validator = SwaggerRequestResponseValidator
        .createFor("my-service-schema.yaml").build();
    final Request validRequest = SimpleRequest.Builder.get("/myService/AL20170730/11111555556161/191919")
        .withHeader("api-key", "TESTKEY")
        .build();
    Response validResponse = SimpleResponse.Builder.ok()
        .withBody(ValidatorTestUtil.loadResponse("my_service_sample_response_2017_03_16")).build();
    ValidationReport reportForText = validator.validate(validRequest, validResponse);
    logger.info(
        "shouldPayloadFromFileConformToSchema() ################# VALIDATION PAYLOAD REPORT ##################");
    reportForText.getMessages().forEach((m) -> {
        logger.info("{}", m);
    });
    assertFalse(reportForText.hasErrors());
    logger.info(
        "shouldPayloadFromFileConformToSchema() ################# VALIDATION PAYLOAD REPORT END ##################");
    logger.info(validRequest.getHeaders().toString());
    SwaggerModule swagger = new SwaggerModule();


}

When This code runs It seems that it goes into the service(as it prints some logs of the service.) but not invokes the method which will the database with the given values.

I need to do something here that it invokes the method of the service and validates the response on the basis of this swagger spec.

I saw this link but didn't get the solution How to validate API in tests with Swagger?

4

1 回答 1

2

如果您希望针对正在运行的服务验证实际交互,我建议您使用 RestAssured 模块(https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-restassured / )

这将允许您对正在运行的服务执行请求,然后验证请求/响应交互是否符合您的 swagger 规范。

在示例模块中有一个使用示例 - https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-examples/src/test/java/com/atlassian/oai /validator/examples/restassured/SwaggerValidationFilterTestExample.java(请注意,那里的示例使用 WireMock 来存根一个真实的服务,但您可以用实际运行的服务替换它)。

于 2017-12-15T02:31:55.240 回答