Giter Club home page Giter Club logo

spring-cloud-contrac's Introduction

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:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-contract-verifier</artifactId>
    <scope>test</scope>
</dependency>

Your contract test example

create this contract in java/test/contracts path.

package contracts

org.springframework.cloud.contract.spec.Contract.make {

    description("Should return all customer")
    request {
        method('GET')
        urlPath('/example')
        headers {
            contentType('application/json')
        }
    }
    response {
        status(200)
        headers {
            contentType('application/json')
        }
        body("""
              {
                "name": "user",
                "fullName": "fullName",
                "phone": "987654321"
              }
        """)
    }
}

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

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:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
    <scope>test</scope>
</dependency>

Your test consumer stubs

These anotation you must use when define a test contract:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureStubRunner(stubsMode = StubsMode.LOCAL, ids ="com.cloud:contract-producer:+:stubs:8091")
class ContractTestExample { 

    // your test code.

}

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.

spring-cloud-contrac's People

Contributors

r3m25 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.