I want to write a contract using spring cloud contract in producer API.
Here is my API:
@PostMapping(path = "/person",
produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<PersonInfo> test(@RequestBody PersonRequest request){
PersonInfo personInfo = bankService.getPersonInfo(request);
return new ResponseEntity<>(personInfo, HttpStatus.OK);
}
My DSL in groovy :
Contract.make {
description("spring cloud contract")
request {
method 'POST'
url '/person'
headers {
contentType(applicationJson())
}
body("""
{
"nationalId": "2548745"
}
""")
}
response {
status 200
body("""
{
"name": "Marc",
"family": "Brown",
"gender": "M"
}
""")
headers {
contentType(applicationJson())
}
}
}
PersonRequest :
public class PersonRequest {
private String nationalId;
public String getNationalId() {
return nationalId;
}
public void setNationalId(String nationalId) {
this.nationalId = nationalId;
}
}
Now when I clean and install the project I get this error :
Expecting: <415> to be equal to: <200> but was not.
If I change @RequestBody PersonRequest request
to @RequestBody String request
everything works fine.
I am using spring boot 2.3.1.RELEASE and spring-cloud-starter-contract-verifier 2.2.3.RELEASE and junit5
I test this case with junit4 and it is ok. How can I fix this problem?
Don't change the dsl, change the base class
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig() .encoderConfig(new EncoderConfig(UTF_8.name(), UTF_8.name()));
or opt-out appending of default content charset:
EncoderConfig encoderConfig = new EncoderConfig() .appendDefaultContentCharsetToContentTypeIfUndefined(false); RestAssuredMockMvcConfig restAssuredConf = new RestAssuredMockMvcConfig() .encoderConfig(encoderConfig); RestAssuredMockMvc.config = restAssuredConf;