项目作者: r3m25

项目描述 :
Consumer drive contract with Spring Cloud Contract.
高级语言: Java
项目地址: git://github.com/r3m25/spring-cloud-contrac.git
创建时间: 2020-07-13T03:00:01Z
项目社区:https://github.com/r3m25/spring-cloud-contrac

开源协议:

下载


spring-cloud-contrac

In this sampple you can see a useful implementation with spring cloud contract. In this repo you can find two projects, producer and consumer.

Getting Started

Producer

to start work with Spring Cloud Contract in producer side, you have to add in your pom.xml this dependency below:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-contract-verifier</artifactId>
  4. <scope>test</scope>
  5. </dependency>

Your contract test example

create this contract in java/test/contracts path.

  1. package contracts
  2. org.springframework.cloud.contract.spec.Contract.make {
  3. description("Should return all customer")
  4. request {
  5. method('GET')
  6. urlPath('/example')
  7. headers {
  8. contentType('application/json')
  9. }
  10. }
  11. response {
  12. status(200)
  13. headers {
  14. contentType('application/json')
  15. }
  16. body("""
  17. {
  18. "name": "user",
  19. "fullName": "fullName",
  20. "phone": "987654321"
  21. }
  22. """)
  23. }
  24. }

after create your contract, if you’re using maven, you have to execute this commad:

  1. mvn clean install -DskipTests

this command generate a stubs jar in target/ folder and mapping wiremock in target/stubs/*

Consumer

After to create your producer side, you can use Spring Cloud Contract Stub Runner in the integration tests to get a running WireMock instance or messaging route that simulates the actual service.

To get started, add the dependency to Spring Cloud Contract Stub Runner, as follows:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
  4. <scope>test</scope>
  5. </dependency>

Your test consumer stubs

These anotation you must use when define a test contract:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. @AutoConfigureMockMvc
  4. @AutoConfigureStubRunner(stubsMode = StubsMode.LOCAL, ids ="com.cloud:contract-producer:+:stubs:8091")
  5. class ContractTestExample {
  6. // your test code.
  7. }

When you use @AutoConfigureStubRunner anotation, it looks for a stub jar that has been generetaed since producer side in repository jar, for example:

  • if you are working in local env and you are unisig .m2, you should use StubsMode.LOCAL.

  • if you are unsing a remote repo like Artifactory for example, you should use StubsMode.REMOTE.