嗯,我被困在這種情況下,我想使用Swagger API來驗證我的Lagom服務API的響應。 下面是一些示例代碼:(它打印服務的一些日誌)如何使用SwaggerValidator(com.atlassian.oai.validator)驗證Lagom API響應?
@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();
}
運行此代碼時它似乎進入服務而不是調用該方法將與給定值的數據庫。
我需要在這裏做一些事情,它調用服務的方法並根據這個swagger規範驗證響應。
我看到這個鏈接,但如果你正在尋找驗證對正在運行的服務的真實互動,我建議使用RestAssured模塊(https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-restassured/)
沒有得到解決How to validate API in tests with Swagger?