Spring Cloud Cloud Architecture-Contract Introduction

Source: Internet
Author: User
Tags apache camel

just for a brief explanation-Spring Cloud Contract Validator is a tool that enables consumer-driven contracts (CDC) to develop JVM-based applications. It is provided in conjunction with the contract definition Language (DSL) . a contract definition is used to generate the following resources:

    • in the client code ( Client Testing ) Wiremock will use the JSON stub to define the integration test on the . The test code still needs to be written manually, and the test data is generated by the spring Cloud contract validator.

    • message-Passing routing if you use one. We are integrating with Spring integration,spring Cloud stream,spring amqp and Apache Camel. However, you can set your own integration if you want to

    • Acceptance Testing (in JUnit or Spock) is used to verify that the server-side implementation of the API is compliant with the contract ( Server Testing ). The full test is generated by the spring Cloud contract validator.

Spring Cloud Contract verifier moves TDD to the level of the software architecture.

In order to use the spring Cloud contract authenticator in Wiremock, you must use the Gradle or Maven plugin.

add a gradient plug-in with dependencies
buildscript {repositories {mavenCentral()}dependencies {    classpath "org.springframework.boot:spring-boot-gradle-plugin:${springboot_version}"classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:${verifier_version}"}}apply plugin: ‘groovy‘apply plugin: ‘spring-cloud-contract‘dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${verifier_version}"}}dependencies {testCompile ‘org.codehaus.groovy:groovy-all:2.4.6‘// example with adding Spock core and Spock SpringtestCompile ‘org.spockframework:spock-core:1.0-groovy-2.4‘testCompile ‘org.spockframework:spock-spring:1.0-groovy-2.4‘testCompile ‘org.springframework.cloud:spring-cloud-starter-contract-verifier‘}
snapshot version of Gradle

Add additional snapshot repositories to your build.gradle to use the snapshot version, which is automatically uploaded after each successful build:

buildscript {repositories {mavenCentral()mavenLocal()maven { url "http://repo.spring.io/snapshot" }maven { url "http://repo.spring.io/milestone" }maven { url "http://repo.spring.io/release" }}}
Add stub

By default, the Spring Cloud contract validator is looking for src/test/resources/contracts stubs in the directory.

directories that contain stub definitions are treated as a class name, and each stub definition is treated as a single test. We assume that it contains at least one directory that is used as the test class name. If there are multiple levels of nested directories, the last level will be used as the package name. So it has the following structure

src/test/resources/contracts/myservice/shouldCreateUser.groovysrc/test/resources/contracts/myservice/shouldReturnUser.groovy

Spring Cloud Contract Validator will create a test class using two methodsdefaultBasePackage.MyService

    • shouldCreateUser()

    • shouldReturnUser()

Run plug-ins

the plugin registers itself check before the task is called. as long as you want it to be part of the build process, you do nothing. If you only want to build the test, call the generateContractTests task.

Default Settings

The default Gradle plug-in setting creates a build of the following Gradle section (it is a pseudo-code)

Contracts {targetframework = ' JUNIT ' testmode = ' mockmvc ' Generatedtestsourcesdir = Project.file ("${project.buil    Ddir}/generated-test-sources/contracts ") Contractsdsldir =" ${project.rootdir}/src/test/resources/contracts " basepackagefortests = ' org.springframework.cloud.verifier.tests ' Stubsoutputdir = Project.file ("${project.builddir} /stubs ")//The following properties is used when you want to provide where the JAR with contract lays Contractdepe ndency {stringnotation = '} Contractspath = ' Contractsworkoffline = false}tasks.create (Type:jar, Nam E: ' Verifierstubsjar ', dependsOn: ' Generateclientstubs ') {baseName = project.name classifier = Contracts.stubssuffi X from contractverifier.stubsoutputdir}project.artifacts {archives task}tasks.create (type:copy, Name: ' CopyContrac ts ') {from contracts.contractsdsldir to Contracts.stubsoutputdir}verifierstubsjar.dependson ' copyContracts ' Publis Hing {publications {StuBS (mavenpublication) {Artifactid project.name artifact Verifierstubsjar}}} 
Sources of information and source

Spring Cloud Cloud Architecture-Contract Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.