I am new in Karatate contract testing. I have a spring boot application, and I wrote a Karate test for it like this:
Background:
* call read('docker-operations.feature')
* url 'http://localhost:8080'
* def path = 'myController/filter'
* def openApiSpec = read('schemas/filtering.json')
* def requestSchema = openApiSpec.components.schemas.filtering_request
* def responseSchema = openApiSpec.components.schemas.filtering_200_response
# needs to be converted to String because if it stays as JSON, karate will automatically convert that JSON into Map in Java class
* def requestSchemaString = karate.toString(requestSchema)
* def responseSchemaString = karate.toString(responseSchema)
* def validRequest = read('schemas/valid-request.json')
* def invalidRequest = read('schemas/invalid-request.json')
* def invalidResponse = read('schemas/invalid-response.json')
Scenario: Valid Response Scenario
Given path path
And request validRequest
When method POST
Then status 200
* print 'Schema JSON:', responseSchema
* print 'Response JSON:', response
* def responseString = karate.toString(response)
And assert Java.type('feature.JsonValidator').isValid(responseSchemaString, responseString)
Just to mention that I am using generated openAPI specification(filtering.json). But I have this situation: I need to run test with current API version, with previous API version and with all previous API versions that we are supporting. How to handle that situation with Karate, if that is possible? I know that with SPring cloud I can do that via Maven for example.
I don't think this is reasonably expected from Karate, it depends a lot on your environment and how you can set up the previous and current versions. Maybe a data-driven test where you loop over all versions is the way to go.
Consider this not directly supported by Karate, but I know teams that have written some routines over Karate to get this to work. We welcome contributions to Karate, it is open source.
Personally, I consider the returns you get from just testing schemas (which is what you seem to be doing) not really worth it: https://www.linkedin.com/pulse/api-contract-testing-visual-guide-peter-thomas/