问题
As it is stated in the spring docs that
cloud contracts help users in successfully implementing the Consumer Driven Contracts approach.
and I've read this in another blog
The essence of the contract tests is not to assert the functionality. What we want to achieve is to verify the semantics. If the producer and the consumer will be able to successfully communicate on production.
I started implementing them and writing code on the producer side , my controller class is:
@PostMapping(path = "/getone", consumes = "application/json", produces = "application/json")
public ResponseEntity<Response> getone(@RequestBody Test[] test) {
String appId = Utils.getId();
return new ResponseEntity<>(service.pub(test,id), HttpStatus.OK);
}
My base class for contract testing:
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@SpringBootTest
public class ContractTest {
@Mock
private Service service;
@Mock
private Utils utils;
@BeforeEach
void static setup(@Autowired Controller controller) {
try (MockedStatic mocked = mockStatic(Utils.class)) {
mocked.when(() -> Utils.getId()).thenReturn("194");
Mockito.when(service.pub(anyString(),anyString())).thenReturn(TestUtils.publish_success_response());
controller.setPublishService(service);
StandaloneMockMvcBuilder standaloneMockMvcBuilder = MockMvcBuilders.standaloneSetup(controller);
RestAssuredMockMvc.standaloneSetup(standaloneMockMvcBuilder);
}
My question is,
1.How are the contracts actually helping my API? If I'm mocking my underlying implementation of service, it is always possible that the developers update the actual implementation in the source code and forget to update the contracts. In this case the contracts would still run against the producer using the Mocked service and pass if the source code changes? How would this guarantee the stability of the API to the consumers ?
2.Should we create contracts for different behaviors of the API depending on the input or just one contract for positive case and one contract for negative case ?
来源:https://stackoverflow.com/questions/64883754/what-are-we-actually-testing-using-spring-cloud-contracts