From fb61c55350b12edfde978b268e652d49fe668286 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 22 Mar 2018 12:21:41 +0100 Subject: [PATCH] Polished index.html - updated content with 2.0.0 change (for stub runner) - added missing plugin setup for producer part - removed info about customization (that's completely irrelevant for quick start) - added info about polyglot fixes gh-553 --- index.html | 92 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index 5614c80c2c..f0f3b0cec4 100644 --- a/index.html +++ b/index.html @@ -32,17 +32,18 @@ badges: Spring Cloud Contract is an umbrella project holding solutions that help users in successfully implementing the [Consumer Driven Contracts](http://martinfowler.com/articles/consumerDrivenContracts.html) approach. Currently Spring Cloud Contract consists of the Spring Cloud Contract Verifier project. -Spring Cloud Contract Verifier is a tool that enables Consumer Driven Contract (CDC) development of JVM-based applications. It is shipped with Contract Definition Language (DSL) written in Groovy. Stating with version 1.1.0 you can define your own way of defining contracts - the only thing you have to provide is a converter. Contract definitions are used to produce following resources: +Spring Cloud Contract Verifier is a tool that enables Consumer Driven Contract (CDC) development of JVM-based applications. It is shipped with Contract Definition Language (DSL) written in Groovy or YAML. Contract definitions are used to produce following resources: -* by default JSON stub definitions to be used by [WireMock](http://wiremock.org) (HTTP Server Stub) when doing integration testing on the client code (client tests). Test code must still be written by hand, test data is produced by Spring Cloud Contract Verifier. Starting with version 1.1.0 you can -provide your own implementation of the HTTP Server Stub. +* by default JSON stub definitions to be used by [WireMock](http://wiremock.org) (HTTP Server Stub) when doing integration testing on the client code (client tests). Test code must still be written by hand, test data is produced by Spring Cloud Contract Verifier. * Messaging routes if you’re using one. We’re integrating with Spring Integration, Spring Cloud Stream and Apache Camel. You can however set your own integrations if you want to. -* Acceptance tests (by default in JUnit or Spock) used to verify if server-side implementation of the API is compliant with the contract (server tests). Full test is generated by Spring Cloud Contract Verifier. Starting with version 1.1.0 you can provide your own way of generating tests (e.g. in a different language). +* Acceptance tests (by default in JUnit or Spock) used to verify if server-side implementation of the API is compliant with the contract (server tests). Full test is generated by Spring Cloud Contract Verifier. Spring Cloud Contract Verifier moves TDD to the level of software architecture. +To see how Spring Cloud Contract supports other languages just check out this [blog post](https://spring.io/blog/2018/02/13/spring-cloud-contract-in-a-polyglot-world). + {% endcapture %} {% capture main_content %} @@ -122,7 +123,7 @@ a `FraudDetectionController` that you're writing) ```java package com.example; -import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc; +// imports public class MvcTest { @@ -134,7 +135,7 @@ public class MvcTest { } ``` -In the `src/test/resources/contracts` add a Stub definition. For example named `shouldMarkClientAsFraud.groovy` +In the `src/test/resources/contracts` add a Contract definition. For example named `shouldMarkClientAsFraud.groovy` ```groovy org.springframework.cloud.contract.spec.Contract.make { @@ -166,21 +167,63 @@ response { } ``` +If you have to add a plugin that will generate tests and stubs for you. + +Maven + +```xml + + + + org.springframework.cloud + spring-cloud-contract-maven-plugin + ${spring-cloud-contract.version} + true + + com.example.MvcTest + + + + +``` + +Gradle + +```groovy +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootPluginVersion}" + classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:${springCloudContractVersion}" + } +} + +apply plugin: 'spring-boot' +apply plugin: 'spring-cloud-contract' + +dependencyManagement { + imports { + mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${springCloudDependencies}" + } +} + +dependencies { + testCompile "org.springframework.cloud:spring-cloud-starter-contract-verifier" +} + +contracts { + baseClassForTests = 'com.example.MvcTest' +} +``` + Once you try to build your application from your contracts tests will be generated in the output folder under `/generated-test-sources/contracts`. ```java package org.springframework.cloud.contract.verifier.tests; -import com.example.MvcTest; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification; -import com.jayway.restassured.response.ResponseOptions; -import org.junit.Test; - -import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*; -import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson; -import static org.assertj.core.api.Assertions.assertThat; +// imports public class ContractVerifierTest extends MvcTest { @@ -258,11 +301,6 @@ Maven - - org.springframework.cloud - spring-cloud-contract-wiremock - test - org.springframework.cloud spring-cloud-starter-contract-stub-runner @@ -292,7 +330,6 @@ dependencyManagement { } dependencies { - testCompile "org.springframework.cloud:spring-cloud-contract-wiremock" testCompile "org.springframework.cloud:spring-cloud-starter-contract-stub-runner" } ``` @@ -301,6 +338,8 @@ The last step is to setup Stub Runner in your tests to automatically download th you have to pass the `@AutoConfigureStubRunner` annotation. That annotation has a bunch of properties that you can set. If you don't like such an approach you can set those values in your test properties too. +For Spring Cloud Contract 1.2.x + ```java @RunWith(SpringRunner.class) @SpringBootTest @@ -308,6 +347,15 @@ set. If you don't like such an approach you can set those values in your test pr public class LoanApplicationServiceTests { ``` +For Spring Cloud Contract starting from 2.0.x + +```java +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureStubRunner(ids = {"com.example:http-server:+:stubs:8080"}, stubsMode = StubRunnerProperties.StubsMode.LOCAL) +public class LoanApplicationServiceTests { +``` + That way an artifact with group id `com.example`, artifact id `http-server`, in `latest` version, with `stubs` classifier will be registered at port `8080`. Since the `workOffline` flag was passed then the stubs will not be downloaded from a remote repository - it will be searched for in the local Maven repo. Once your test context got booted up, executing the following code will not lead to a 404 because the Spring Cloud Contract Stub Runner will automatically start a WireMock server inside your test and feed it with the stubs generated from the server side. ```java