spring contract for cdc

in the beginning, i was confused by why it’s called CDC (consumer driven testing). because in the end, the contract is put at the producer side, and protect the producer making any changes to break the contract.

after looking into this into more detail, then it turns out the contract is supposed to be written by consumer, hence the name of consumer driven.

anyway, its ultimately stopping the producer from making any breaking change which violate the contract.

the gist of the changes:

on the producer side:

  1. add the dependency

org.springframework.cloud
spring-cloud-starter-contract-verifier
test

2. add the plugin, this generates the test, which bind the producer to the contract

org.springframework.cloud
spring-cloud-contract-maven-plugin
${spring-cloud-contract.version}
true

com.example.contractTest.BaseTestClass

org.springframework.boot
spring-boot-maven-plugin

3. add the contract and the base class

on the consumer side:

  1. add the dependency (stub runner and wiremock)
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
    <scope>test</scope>
</dependency>

2. add the test (pointing to the stub)

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:6565"},
        stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class LoanApplicationServiceTests {
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s