'Spring Cloud Contract generated tests fails on empty responses from producer
I am trying to implement Spring Cloud Contract to my project. I am following instructions from this baeldung article: https://www.baeldung.com/spring-cloud-contract
- Dependencies are added
- Plugin is configured
- Producer contract is defined
- BaseTest is defined
Unfortunately my generated tests fails because the response (jsonBody) is "empty"
Here's a few pieces of the setup:
BaseContractTest =>
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMessageVerifier
@DirtiesContext
@AutoConfigureStubRunner(ids = "com.example:producer-service:+:stubs:8080",
stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class BaseContractTest
{
@Autowired
private WebApplicationContext webApplicationContext;
@BeforeEach
void setUp()
{
final DefaultMockMvcBuilder defaultMockMvcBuilder =
MockMvcBuilders.webAppContextSetup(webApplicationContext);
defaultMockMvcBuilder.apply(
springSecurity((request, response, chain) -> chain.doFilter(request, response)));
RestAssuredMockMvc.mockMvc(defaultMockMvcBuilder.build());
}
contract =>
Contract.make {
description "GetCustomer should return a Customer"
request {
method GET()
url value(consumer(regex('/producer-service/v1/customer/ID-\\d*-\\d*')), producer("/producer-service/customer/ID-132456-9876"))
}
response {
status OK()
body(
id: "ID-132456-9876", name: "exampleName"
)
headers {
contentType(applicationJson())
}
}
}
wiremocks mapping are properly generated (omitted for brevity)
Generated ContractTest =>
public class ContractVerifierTest extends BaseContractTest {
@Test
public void validate_shouldReturnACustomer() throws Exception {
// given:
MockMvcRequestSpecification request = given();
// when:
ResponseOptions response = given().spec(request)
.get("/producer-service/v1/ID-132456-9876");
// then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).matches("application/json.*");
// and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).field("['id']").isEqualTo("ID-132456-9876");
assertThatJson(parsedJson).field("['name']").isEqualTo("exampleName");
}
}
when the test runs, it fails with this error:
validate_shouldReturnACustomer Time elapsed: 0.731 s <<< FAILURE!
java.lang.AssertionError:
Expecting actual not to be null
at com.example.contracts.ContractVerifierTest.validate_shouldReturnACustomer(ContractVerifierTest.java:31)
When I look up the corresponding error line, it fails on =>
assertThat(response.header("Content-Type")).matches("application/json.*");
I am a bit clueless at this point.
I tried to use the MockStandaloneApp tied to the controller (as per the link to baeldung) but that did not help.
Note that the service returns a Mono<Customer>
not an actual Customer
, if that changes anything.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|