Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate API in tests with Swagger?

I'm trying to figure out the best way to have my API documentation be the source of truth and use it to validate the actual Java REST code ideally through integration testing or something of that sort. We're using the contract first or consumer contract type of approach, so we don't want the documentation to be generated from annotated code necessarily and updating every time a developer makes a change.

One thought has been to use Swagger, but I'm not sure how best to make it be used for validating the API. Ideally, it'd be good to have the validation occur in the build or integration testing process to see if the real response (and request if possible) match what's expected. I know there are a lot of uses and tools for Swagger and just trying to wrap my head around it. Or if there is a better alternative to work with Java code.

like image 785
whatsTheDiff Avatar asked Jan 15 '16 18:01

whatsTheDiff


3 Answers

I think you should try swagger-request-validator: https://bitbucket.org/atlassian/swagger-request-validator

Here are some examples how to use it: https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-examples/

Another alternative is assertj-swagger: https://github.com/RobWin/assertj-swagger

like image 146
Dennis Kieselhorst Avatar answered Oct 09 '22 22:10

Dennis Kieselhorst


Recently, we (swagger-codegen community) start adding automatic test case generation to API clients (C#, PHP, Ruby). We've not added that to Java yet. Here are some example test cases generated by Swagger-Codegen for C#:

https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test

It's still very preliminary and we would like to hear feedback from you to see if that's what you're looking for.

like image 5
William Cheng Avatar answered Oct 09 '22 21:10

William Cheng


You may want to look at Spring Cloud Contract. It offers you a DSL, where you can describe the scenarios (more or less what is the response I get for a given request) and it seems to fit well to what you described as a requirement...

like image 2
The Ancient Avatar answered Oct 09 '22 20:10

The Ancient