Test endpoints compliance against openapi contract in Spring Boot Rest

谁都会走 提交于 2021-02-11 15:24:58

问题


I am looking for a nice way to write tests to make sure that enpoints in Spring Boot Rest (ver. 2.1.9) application follows the contract in openapi contract.

In the project I moved recently there is following workflow: architects write contract openapi.yml and developers have to implement endpoint to compliance the contract. Unfortunately a lot of differences happen and this test have to catch such situation and it is not possible to change this :(

I was thinking about the solution to generate openapi.yml from current ednpoints and compares it somehow but wonder if there is some out of the box solution.


回答1:


I was thinking about the solution to generate openapi.yml from current ednpoints and compares it somehow but wonder if there is some out of the box solution.

In a general case, even the generated spec may not match the actual app behavior because some things can't be expressed with Open API. However, it still could be helpful as a starting point.

Open API provides a way to specify examples, that could be used to verify the contract. But the actual schemas might be a better source of expectations.

I want to note two tools that can generate and execute test cases based only on the input Open API spec:

  1. Schemathesis uses both examples and schemas and doesn't require configuration by default. It utilizes property-based testing and verifies properties defined in the tested schema - response codes, schemas, and headers. It supports Open API 2 & 3.
  2. Dredd focuses more on examples and provides several automatic expectations. It supports only Open API 2, and the third version is experimental.

Both provide a CLI and could be extended with various hooks to fit the desired workflow.

I'd suggest passing the contracts (as a spec you mentioned) to Schemathesis and it will verify if all schemas and examples are handled correctly by your app.



来源:https://stackoverflow.com/questions/59141014/test-endpoints-compliance-against-openapi-contract-in-spring-boot-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!