spring-cloud-contract: Wrong collection assertion method is chosen for SpringCloudContractAssertions
We have created Contract with testMatchers according to the documentation. But when tests are generated then wrong assertions are created. We are using version 1.0.5 because 1.1.2 have to many changes for example added root folder to stubsOutputDir.
For example we have
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'GET'
urlPath(path)
}
response {
status 200
headers {
header 'Content-Type': value(test(regex('application/json(;.*)?')), stub('application/json;charset=UTF-8'))
}
body(events: [[
operation : 'EXPORT',
eventId : '16f1ed75-0bcc-4f0d-a04d-3121798faf99',
status : 'OK'
], [
operation : 'INPUT_PROCESSING',
eventId : '3bb4ac82-6652-462f-b6d1-75e424a0024a',
status : 'OK'
]
]
)
testMatchers {
jsonPath('$.events[0].operation', byRegex('.+'))
jsonPath('$.events[0].eventId', byRegex('^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})$'))
jsonPath('$.events[0].status', byRegex('.+'))
}
}
}
and generated test looks like
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import static com.jayway.restassured.RestAssured.*
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson
class EventSpec {
def validate_listEvents() throws Exception {
given:
def request = given()
when:
def response = given().spec(request)
.get(urlPath")
then:
response.statusCode == 200
response.header('Content-Type') ==~ java.util.regex.Pattern.compile('application/json(;.*)?')
and:
DocumentContext parsedJson = JsonPath.parse(response.body.asString())
assertThatJson(parsedJson).array("['events']").contains("['eventId']").isEqualTo("16f1ed75-0bcc-4f0d-a04d-3121798faf99")
assertThatJson(parsedJson).array("['events']").contains("['operation']").isEqualTo("EXPORT")
assertThatJson(parsedJson).array("['events']").contains("['operation']").isEqualTo("INPUT_PROCESSING")
assertThatJson(parsedJson).array("['events']").contains("['eventId']").isEqualTo("3bb4ac82-6652-462f-b6d1-75e424a0024a")
assertThatJson(parsedJson).array("['events']").contains("['status']").isEqualTo("OK")
and:
assertThat(parsedJson.read("\$.events[0].operation", String.class)).matches(".+")
assertThat(parsedJson.read("\$.events[0].eventId", String.class)).matches("^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\$")
assertThat(parsedJson.read("\$.events[0].status", String.class)).matches(".+")
}
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 26 (14 by maintainers)
Commits related to this issue
- Wrong assertion type is picked for Iterables without this change in the generated test when SpringCloudContractAssertions are used, we're casting the read element of the JSON to a Collection class wh... — committed to spring-cloud/spring-cloud-contract by marcingrzejszczak 7 years ago
- Wrong assertion type is picked for Iterables without this change in the generated test when SpringCloudContractAssertions are used the compiler is picking an invalid type of Assertj Assertions. with ... — committed to spring-cloud/spring-cloud-contract by marcingrzejszczak 7 years ago
- Wrong assertion type is picked for Iterables without this change in the generated test when SpringCloudContractAssertions are used, we're casting the read element of the JSON to a Collection class wh... — committed to spring-cloud/spring-cloud-contract by marcingrzejszczak 7 years ago
- Wrong assertion type is picked for Iterables without this change in the generated test when SpringCloudContractAssertions are used the compiler is picking an invalid type of Assertj Assertions. with ... — committed to spring-cloud/spring-cloud-contract by marcingrzejszczak 7 years ago
1.1.2.RELEASE