diff --git a/multi/multi__spring_cloud_contract_verifier_introduction.html b/multi/multi__spring_cloud_contract_verifier_introduction.html index f23350369e..31ded5b55b 100644 --- a/multi/multi__spring_cloud_contract_verifier_introduction.html +++ b/multi/multi__spring_cloud_contract_verifier_introduction.html @@ -378,7 +378,7 @@ You would like to feed that instance with a proper stub definition.

At som restTemplate.exchange("http://localhost:" + port + "/fraudcheck", HttpMethod.PUT, new HttpEntity<>(request, httpHeaders), FraudServiceResponse.class);

Annotate your test class with @AutoConfigureStubRunner. In the annotation provide the group id and artifact id for the Stub Runner to download stubs of your collaborators.

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

After that, during the tests, Spring Cloud Contract automatically finds the stubs @@ -674,7 +674,7 @@ Application service):

Add the Spring Cloud Co group-id and artifact-id for the Stub Runner to download the stubs of your collaborators. (Optional step) Because you’re playing with the collaborators offline, you can also provide the offline work switch (StubRunnerProperties.StubsMode.LOCAL).

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

Now, when you run your tests, you see something like this:

2016-07-19 14:22:25.403  INFO 41050 --- [           main] o.s.c.c.stubrunner.AetherStubDownloader  : Desired version is + - will try to resolve the latest version
@@ -713,9 +713,8 @@ capitalize fraud. That gives you the Rest Assured MVC to
 start the server side FraudDetectionController.

package com.example.fraud;
 
-import org.junit.Before;
-
 import io.restassured.module.mockmvc.RestAssuredMockMvc;
+import org.junit.Before;
 
 public class FraudBase {
 	@Before
diff --git a/multi/multi__spring_cloud_contract_verifier_setup.html b/multi/multi__spring_cloud_contract_verifier_setup.html
index 24261cb69a..08185f0e04 100644
--- a/multi/multi__spring_cloud_contract_verifier_setup.html
+++ b/multi/multi__spring_cloud_contract_verifier_setup.html
@@ -71,6 +71,7 @@ pseudocode):

contracts {
     testFramework ='JUNIT'
     testMode = 'MockMvc'
     generatedTestSourcesDir = project.file("${project.buildDir}/generated-test-sources/contracts")
+    generatedTestResourcesDir = project.file("${project.buildDir}/generated-test-resources/contracts")
     contractsDslDir = "${project.rootDir}/src/test/resources/contracts"
     basePackageForTests = 'org.springframework.cloud.verifier.tests'
     stubsOutputDir = project.file("${project.buildDir}/stubs")
@@ -131,7 +132,9 @@ classes.
  • ignoredFiles
  • contractsDslDir: Specifies the directory containing contracts written using the GroovyDSL. By default, its value is $rootDir/src/test/resources/contracts.
  • generatedTestSourcesDir: Specifies the test source directory where tests generated from the Groovy DSL should be placed. By default its value is -$buildDir/generated-test-sources/contractVerifier.
  • stubsOutputDir: Specifies the directory where the generated WireMock stubs from +$buildDir/generated-test-sources/contracts.
  • generatedTestResourcesDir: Specifies the test resource directory where resources used by the tests generated +from the Groovy DSL should be placed. By default its value is +$buildDir/generated-test-resources/contracts.
  • stubsOutputDir: Specifies the directory where the generated WireMock stubs from the Groovy DSL should be placed.
  • testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and JUnit 5 are supported with JUnit 4 being the default framework.
  • contractsProperties: a map containing properties to be passed to Spring Cloud Contract components. Those properties might be used by e.g. inbuilt or custom Stub Downloaders.
  • The following properties are used when you want to specify the location of the JAR @@ -363,7 +366,9 @@ If neither of these values are set, then the value is set to org.springframework.cloud.contract.verifier.tests.

  • ruleClassForTests: Specifies a rule that should be added to the generated test classes.
  • baseClassForTests: Creates a base class for all generated tests. By default, if you use Spock classes, the class is spock.lang.Specification.
  • contractsDirectory: Specifies a directory containing contracts written with the -GroovyDSL. The default directory is /src/test/resources/contracts.
  • testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and +GroovyDSL. The default directory is /src/test/resources/contracts.
  • generatedTestSourcesDir: Specifies the test source directory where tests generated +from the Groovy DSL should be placed. By default its value is +$buildDir/generated-test-sources/contracts.
  • generatedTestResourcesDir: Specifies the test resource directory where resources used by the tests generated
  • testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and JUnit 5 are supported with JUnit 4 being the default framework.
  • packageWithBaseClasses: Defines a package where all the base classes reside. This setting takes precedence over baseClassForTests. The convention is such that, if you have a contract under (for example) src/test/resources/contract/foo/bar/baz/ and set diff --git a/multi/multi__using_the_pluggable_architecture.html b/multi/multi__using_the_pluggable_architecture.html index 8e786b74af..d4997c0d16 100644 --- a/multi/multi__using_the_pluggable_architecture.html +++ b/multi/multi__using_the_pluggable_architecture.html @@ -231,37 +231,53 @@ current Pact version that you use.

    Maven. 

    testCompile "org.springframework.cloud:spring-cloud-contract-pact"

    10.2 Using the Custom Test Generator

    If you want to generate tests for languages other than Java or you are not happy with the way the verifier builds Java tests, you can register your own implementation.

    The SingleTestGenerator interface lets you register your own implementation. The -following code listing shows the SingleTestGenerator interface:

    package org.springframework.cloud.contract.verifier.builder
    -
    -import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
    -import org.springframework.cloud.contract.verifier.file.ContractMetadata
    -/**
    - * Builds a single test.
    - *
    - * @since 1.1.0
    - */
    -interface SingleTestGenerator {
    +following code listing shows the SingleTestGenerator interface:

    	 *
    +	 * @param properties                    - properties passed to the plugin
    +	 * @param listOfFiles                   - list of parsed contracts with additional metadata
    +	 * @param className                     - the name of the generated test class
    +	 * @param classPackage                  - the name of the package in which the test class should be stored
    +	 * @param includedDirectoryRelativePath - relative path to the included directory
    +	 * @return contents of a single test class
    +	 * @deprecated use {@link SingleTestGenerator#buildClass(ContractVerifierConfigProperties, Collection, String, GeneratedClassData)}
    +	 */
    +	@Deprecated
    +	String buildClass(ContractVerifierConfigProperties properties,
    +			Collection<ContractMetadata> listOfFiles, String className, String classPackage, String includedDirectoryRelativePath);
     
     	/**
     	 * Creates contents of a single test class in which all test scenarios from
     	 * the contract metadata should be placed.
     	 *
    -	 * @param properties - properties passed to the plugin
    -	 * @param listOfFiles - list of parsed contracts with additional metadata
    -	 * @param className - the name of the generated test class
    -	 * @param classPackage - the name of the package in which the test class should be stored
    +	 * @param properties                    - properties passed to the plugin
    +	 * @param listOfFiles                   - list of parsed contracts with additional metadata
    +	 * @param generatedClassData            - information about the generated class
     	 * @param includedDirectoryRelativePath - relative path to the included directory
     	 * @return contents of a single test class
     	 */
    -	String buildClass(ContractVerifierConfigProperties properties, Collection<ContractMetadata> listOfFiles,
    -					  String className, String classPackage, String includedDirectoryRelativePath)
    +	default String buildClass(ContractVerifierConfigProperties properties,
    +			Collection<ContractMetadata> listOfFiles, String includedDirectoryRelativePath, GeneratedClassData generatedClassData) {
    +		return buildClass(properties, listOfFiles, generatedClassData.className, generatedClassData.classPackage, includedDirectoryRelativePath);
    +	}
     
     	/**
     	 * Extension that should be appended to the generated test class. E.g. {@code .java} or {@code .php}
     	 *
     	 * @param properties - properties passed to the plugin
     	 */
    -	String fileExtension(ContractVerifierConfigProperties properties)
    +	String fileExtension(ContractVerifierConfigProperties properties);
    +
    +	class GeneratedClassData {
    +		public final String className;
    +		public final String classPackage;
    +		public final java.nio.file.Path testClassPath;
    +
    +		public GeneratedClassData(String className, String classPackage,
    +				java.nio.file.Path testClassPath) {
    +			this.className = className;
    +			this.classPackage = classPackage;
    +			this.testClassPath = testClassPath;
    +		}
    +	}
     }

    Again, you must provide a spring.factories file, such as the one shown in the following example:

    org.springframework.cloud.contract.verifier.builder.SingleTestGenerator=/
     com.example.MyGenerator

    10.3 Using the Custom Stub Generator

    If you want to generate stubs for stub servers other than WireMock, you can plug in your @@ -422,10 +438,10 @@ to clone the repository and use it as a source of contracts to generate tests or stubs.

    Either via environment variables, system properties, properties set inside the plugin or contracts repository configuration you can tweak the downloader’s behaviour. Below you can find the list of -properties

    Table 10.1. SCM Stub Downloader properties

    Type of a property

    Name of the property

    Description

    * git.branch (plugin prop)

    * stubrunner.properties.git.branch (system prop)

    * STUBRUNNER_PROPERTIES_GIT_BRANCH (env prop)

    master

    Which branch to checkout

    * git.username (plugin prop)

    * stubrunner.properties.git.username (system prop)

    * STUBRUNNER_PROPERTIES_GIT_USERNAME (env prop)

     

    Git clone username

    * git.password (plugin prop)

    * stubrunner.properties.git.password (system prop)

    * STUBRUNNER_PROPERTIES_GIT_PASSWORD (env prop)

     

    Git clone password

    * git.no-of-attempts (plugin prop)

    * stubrunner.properties.git.no-of-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_NO_OF_ATTEMPTS (env prop)

    10

    Number of attempts to push the commits to origin

    * git.wait-between-attempts (Plugin prop)

    * stubrunner.properties.git.wait-between-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_WAIT_BETWEEN_ATTEMPTS (env prop)

    1000

    Number of millis to wait between attempts to push the commits to origin


    10.7 Using the Pact Stub Downloader

    Whenever the repositoryRoot starts with a Pact protocol +properties

    Table 10.1. SCM Stub Downloader properties

    Type of a property

    Name of the property

    Description

    * git.branch (plugin prop)

    * stubrunner.properties.git.branch (system prop)

    * STUBRUNNER_PROPERTIES_GIT_BRANCH (env prop)

    master

    Which branch to checkout

    * git.username (plugin prop)

    * stubrunner.properties.git.username (system prop)

    * STUBRUNNER_PROPERTIES_GIT_USERNAME (env prop)

     

    Git clone username

    * git.password (plugin prop)

    * stubrunner.properties.git.password (system prop)

    * STUBRUNNER_PROPERTIES_GIT_PASSWORD (env prop)

     

    Git clone password

    * git.no-of-attempts (plugin prop)

    * stubrunner.properties.git.no-of-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_NO_OF_ATTEMPTS (env prop)

    10

    Number of attempts to push the commits to origin

    * git.wait-between-attempts (Plugin prop)

    * stubrunner.properties.git.wait-between-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_WAIT_BETWEEN_ATTEMPTS (env prop)

    1000

    Number of millis to wait between attempts to push the commits to origin


    10.7 Using the Pact Stub Downloader

    Whenever the repositoryRoot starts with a Pact protocol (starts with pact://), the stub downloader will try to fetch the Pact contract definitions from the Pact Broker. Whatever is set after pact:// will be parsed as the Pact Broker URL.

    Either via environment variables, system properties, properties set inside the plugin or contracts repository configuration you can tweak the downloader’s behaviour. Below you can find the list of -properties

    Table 10.2. SCM Stub Downloader properties

    Name of a property

    Default

    Description

    * pactbroker.host (plugin prop)

    * stubrunner.properties.pactbroker.host (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_HOST (env prop)

    Host from URL passed to repositoryRoot

    What is the URL of Pact Broker

    * pactbroker.port (plugin prop)

    * stubrunner.properties.pactbroker.port (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PORT (env prop)

    Port from URL passed to repositoryRoot

    What is the port of Pact Broker

    * pactbroker.protocol (plugin prop)

    * stubrunner.properties.pactbroker.protocol (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROTOCOL (env prop)

    Protocol from URL passed to repositoryRoot

    What is the protocol of Pact Broker

    * pactbroker.tags (plugin prop)

    * stubrunner.properties.pactbroker.tags (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_TAGS (env prop)

    Version of the stub, or latest if version is +

    What tags should be used to fetch the stub

    * pactbroker.auth.scheme (plugin prop)

    * stubrunner.properties.pactbroker.auth.scheme (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_SCHEME (env prop)

    Basic

    What kind of authentication should be used to connect to the Pact Broker

    * pactbroker.auth.username (plugin prop)

    * stubrunner.properties.pactbroker.auth.username (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_USERNAME (env prop)

    The username passed to contractsRepositoryUsername (maven) or contractRepository.username (gradle)

    Username used to connect to the Pact Broker

    * pactbroker.auth.password (plugin prop)

    * stubrunner.properties.pactbroker.auth.password (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_PASSWORD (env prop)

    The password passed to contractsRepositoryPassword (maven) or contractRepository.password (gradle)

    Password used to connect to the Pact Broker

    * pactbroker.provider-name-with-group-id (plugin prop)

    * stubrunner.properties.pactbroker.provider-name-with-group-id (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROVIDER_NAME_WITH_GROUP_ID (env prop)

    false

    When true, the provider name will be a combination of groupId:artifactId. If false, just artifactId is used


    \ No newline at end of file +properties

    Table 10.2. SCM Stub Downloader properties

    Name of a property

    Default

    Description

    * pactbroker.host (plugin prop)

    * stubrunner.properties.pactbroker.host (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_HOST (env prop)

    Host from URL passed to repositoryRoot

    What is the URL of Pact Broker

    * pactbroker.port (plugin prop)

    * stubrunner.properties.pactbroker.port (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PORT (env prop)

    Port from URL passed to repositoryRoot

    What is the port of Pact Broker

    * pactbroker.protocol (plugin prop)

    * stubrunner.properties.pactbroker.protocol (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROTOCOL (env prop)

    Protocol from URL passed to repositoryRoot

    What is the protocol of Pact Broker

    * pactbroker.tags (plugin prop)

    * stubrunner.properties.pactbroker.tags (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_TAGS (env prop)

    Version of the stub, or latest if version is +

    What tags should be used to fetch the stub

    * pactbroker.auth.scheme (plugin prop)

    * stubrunner.properties.pactbroker.auth.scheme (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_SCHEME (env prop)

    Basic

    What kind of authentication should be used to connect to the Pact Broker

    * pactbroker.auth.username (plugin prop)

    * stubrunner.properties.pactbroker.auth.username (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_USERNAME (env prop)

    The username passed to contractsRepositoryUsername (maven) or contractRepository.username (gradle)

    Username used to connect to the Pact Broker

    * pactbroker.auth.password (plugin prop)

    * stubrunner.properties.pactbroker.auth.password (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_PASSWORD (env prop)

    The password passed to contractsRepositoryPassword (maven) or contractRepository.password (gradle)

    Password used to connect to the Pact Broker

    * pactbroker.provider-name-with-group-id (plugin prop)

    * stubrunner.properties.pactbroker.provider-name-with-group-id (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROVIDER_NAME_WITH_GROUP_ID (env prop)

    false

    When true, the provider name will be a combination of groupId:artifactId. If false, just artifactId is used


    \ No newline at end of file diff --git a/multi/multi_contract-dsl.html b/multi/multi_contract-dsl.html index 314644b595..0b4aa67391 100644 --- a/multi/multi_contract-dsl.html +++ b/multi/multi_contract-dsl.html @@ -181,7 +181,40 @@ response: bodyFromFile: response.json
  • Further assume that the JSON files is as follows:

    request.json

    { "status" : "REQUEST" }

    response.json

    { "status" : "RESPONSE" }

    When test or stub generation takes place, the contents of the file is passed to the body of a request or a response. The name of the file needs to be a file with location -relative to the folder in which the contract lays.

    8.2.5 HTTP Top-Level Elements

    The following methods can be called in the top-level closure of a contract definition. +relative to the folder in which the contract lays.

    If you need to pass the contents of a file in a binary form +it’s enough for you to use the fileAsBytes method in Groovy DSL or bodyFromFileAsBytes field in YAML.

    Groovy DSL.  +

    import org.springframework.cloud.contract.spec.Contract
    +
    +Contract.make {
    +	request {
    +		url("/1")
    +		method(PUT())
    +		headers {
    +			contentType(applicationOctetStream())
    +		}
    +		body(fileAsBytes("request.pdf"))
    +	}
    +	response {
    +		status 200
    +		body(fileAsBytes("response.pdf"))
    +		headers {
    +			contentType(applicationOctetStream())
    +		}
    +	}
    +}

    +

    YAML.  +

    request:
    +    url: /1
    +    method: PUT
    +    headers:
    +        Content-Type: application/octet-stream
    +    bodyFromFileAsBytes: request.pdf
    +response:
    +    status: 200
    +    bodyFromFileAsBytes: response.pdf
    +    headers:
    +        Content-Type: application/octet-stream

    +

    [Important]Important

    You should use this approach whenever you want to work with binary payloads both for HTTP and messaging.

    8.2.5 HTTP Top-Level Elements

    The following methods can be called in the top-level closure of a contract definition. request and response are mandatory. priority is optional.

    Groovy DSL. 

    org.springframework.cloud.contract.spec.Contract.make {
     	// Definition of HTTP request part of the contract
    @@ -896,16 +929,16 @@ ensure that the authToken() method returns everythi
     JSON path:

    • String: If you point to a String value in the JSON.
    • JSONArray: If you point to a List in the JSON.
    • Map: If you point to a Map in the JSON.
    • Number: If you point to Integer, Double etc. in the JSON.
    • Boolean: If you point to a Boolean in the JSON.

    In the request part of the contract, you can specify that the body should be taken from a method.

    [Important]Important

    You must provide both the consumer and the producer side. The execute part is applied for the whole body - not for parts of it.

    The following example shows how to read an object from JSON:

    Contract contractDsl = Contract.make {
    -    request {
    -        method 'GET'
    -        url '/something'
    -        body(
    -                $(c('foo'), p(execute('hashCode()')))
    -        )
    -    }
    -    response {
    -        status OK()
    -    }
    +	request {
    +		method 'GET'
    +		url '/something'
    +		body(
    +				$(c('foo'), p(execute('hashCode()')))
    +		)
    +	}
    +	response {
    +		status OK()
    +	}
     }

    The preceding example results in calling the hashCode() method in the request body. It should resemble the following code:

    // given:
      MockMvcRequestSpecification request = given()
    diff --git a/single/spring-cloud-contract.html b/single/spring-cloud-contract.html
    index fd48feeb4b..76c97e2fde 100644
    --- a/single/spring-cloud-contract.html
    +++ b/single/spring-cloud-contract.html
    @@ -383,7 +383,7 @@ You would like to feed that instance with a proper stub definition.

    At som restTemplate.exchange("http://localhost:" + port + "/fraudcheck", HttpMethod.PUT, new HttpEntity<>(request, httpHeaders), FraudServiceResponse.class);

    Annotate your test class with @AutoConfigureStubRunner. In the annotation provide the group id and artifact id for the Stub Runner to download stubs of your collaborators.

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

    After that, during the tests, Spring Cloud Contract automatically finds the stubs @@ -679,7 +679,7 @@ Application service):

    Add the Spring Cloud Co group-id and artifact-id for the Stub Runner to download the stubs of your collaborators. (Optional step) Because you’re playing with the collaborators offline, you can also provide the offline work switch (StubRunnerProperties.StubsMode.LOCAL).

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

    Now, when you run your tests, you see something like this:

    2016-07-19 14:22:25.403  INFO 41050 --- [           main] o.s.c.c.stubrunner.AetherStubDownloader  : Desired version is + - will try to resolve the latest version
    @@ -718,9 +718,8 @@ capitalize fraud. That gives you the Rest Assured MVC to
     start the server side FraudDetectionController.

    package com.example.fraud;
     
    -import org.junit.Before;
    -
     import io.restassured.module.mockmvc.RestAssuredMockMvc;
    +import org.junit.Before;
     
     public class FraudBase {
     	@Before
    @@ -1473,6 +1472,7 @@ pseudocode):

    contracts {
         testFramework ='JUNIT'
         testMode = 'MockMvc'
         generatedTestSourcesDir = project.file("${project.buildDir}/generated-test-sources/contracts")
    +    generatedTestResourcesDir = project.file("${project.buildDir}/generated-test-resources/contracts")
         contractsDslDir = "${project.rootDir}/src/test/resources/contracts"
         basePackageForTests = 'org.springframework.cloud.verifier.tests'
         stubsOutputDir = project.file("${project.buildDir}/stubs")
    @@ -1533,7 +1533,9 @@ classes.
  • ignoredFiles
  • contractsDslDir: Specifies the directory containing contracts written using the GroovyDSL. By default, its value is $rootDir/src/test/resources/contracts.
  • generatedTestSourcesDir: Specifies the test source directory where tests generated from the Groovy DSL should be placed. By default its value is -$buildDir/generated-test-sources/contractVerifier.
  • stubsOutputDir: Specifies the directory where the generated WireMock stubs from +$buildDir/generated-test-sources/contracts.
  • generatedTestResourcesDir: Specifies the test resource directory where resources used by the tests generated +from the Groovy DSL should be placed. By default its value is +$buildDir/generated-test-resources/contracts.
  • stubsOutputDir: Specifies the directory where the generated WireMock stubs from the Groovy DSL should be placed.
  • testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and JUnit 5 are supported with JUnit 4 being the default framework.
  • contractsProperties: a map containing properties to be passed to Spring Cloud Contract components. Those properties might be used by e.g. inbuilt or custom Stub Downloaders.
  • The following properties are used when you want to specify the location of the JAR @@ -1765,7 +1767,9 @@ If neither of these values are set, then the value is set to org.springframework.cloud.contract.verifier.tests.

  • ruleClassForTests: Specifies a rule that should be added to the generated test classes.
  • baseClassForTests: Creates a base class for all generated tests. By default, if you use Spock classes, the class is spock.lang.Specification.
  • contractsDirectory: Specifies a directory containing contracts written with the -GroovyDSL. The default directory is /src/test/resources/contracts.
  • testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and +GroovyDSL. The default directory is /src/test/resources/contracts.
  • generatedTestSourcesDir: Specifies the test source directory where tests generated +from the Groovy DSL should be placed. By default its value is +$buildDir/generated-test-sources/contracts.
  • generatedTestResourcesDir: Specifies the test resource directory where resources used by the tests generated
  • testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and JUnit 5 are supported with JUnit 4 being the default framework.
  • packageWithBaseClasses: Defines a package where all the base classes reside. This setting takes precedence over baseClassForTests. The convention is such that, if you have a contract under (for example) src/test/resources/contract/foo/bar/baz/ and set @@ -3630,7 +3634,40 @@ response: bodyFromFile: response.json
  • Further assume that the JSON files is as follows:

    request.json

    { "status" : "REQUEST" }

    response.json

    { "status" : "RESPONSE" }

    When test or stub generation takes place, the contents of the file is passed to the body of a request or a response. The name of the file needs to be a file with location -relative to the folder in which the contract lays.

    8.2.5 HTTP Top-Level Elements

    The following methods can be called in the top-level closure of a contract definition. +relative to the folder in which the contract lays.

    If you need to pass the contents of a file in a binary form +it’s enough for you to use the fileAsBytes method in Groovy DSL or bodyFromFileAsBytes field in YAML.

    Groovy DSL.  +

    import org.springframework.cloud.contract.spec.Contract
    +
    +Contract.make {
    +	request {
    +		url("/1")
    +		method(PUT())
    +		headers {
    +			contentType(applicationOctetStream())
    +		}
    +		body(fileAsBytes("request.pdf"))
    +	}
    +	response {
    +		status 200
    +		body(fileAsBytes("response.pdf"))
    +		headers {
    +			contentType(applicationOctetStream())
    +		}
    +	}
    +}

    +

    YAML.  +

    request:
    +    url: /1
    +    method: PUT
    +    headers:
    +        Content-Type: application/octet-stream
    +    bodyFromFileAsBytes: request.pdf
    +response:
    +    status: 200
    +    bodyFromFileAsBytes: response.pdf
    +    headers:
    +        Content-Type: application/octet-stream

    +

    [Important]Important

    You should use this approach whenever you want to work with binary payloads both for HTTP and messaging.

    8.2.5 HTTP Top-Level Elements

    The following methods can be called in the top-level closure of a contract definition. request and response are mandatory. priority is optional.

    Groovy DSL. 

    org.springframework.cloud.contract.spec.Contract.make {
     	// Definition of HTTP request part of the contract
    @@ -4345,16 +4382,16 @@ ensure that the authToken() method returns everythi
     JSON path:

    • String: If you point to a String value in the JSON.
    • JSONArray: If you point to a List in the JSON.
    • Map: If you point to a Map in the JSON.
    • Number: If you point to Integer, Double etc. in the JSON.
    • Boolean: If you point to a Boolean in the JSON.

    In the request part of the contract, you can specify that the body should be taken from a method.

    [Important]Important

    You must provide both the consumer and the producer side. The execute part is applied for the whole body - not for parts of it.

    The following example shows how to read an object from JSON:

    Contract contractDsl = Contract.make {
    -    request {
    -        method 'GET'
    -        url '/something'
    -        body(
    -                $(c('foo'), p(execute('hashCode()')))
    -        )
    -    }
    -    response {
    -        status OK()
    -    }
    +	request {
    +		method 'GET'
    +		url '/something'
    +		body(
    +				$(c('foo'), p(execute('hashCode()')))
    +		)
    +	}
    +	response {
    +		status OK()
    +	}
     }

    The preceding example results in calling the hashCode() method in the request body. It should resemble the following code:

    // given:
      MockMvcRequestSpecification request = given()
    @@ -5879,37 +5916,53 @@ current Pact version that you use.

    Maven. 

    testCompile "org.springframework.cloud:spring-cloud-contract-pact"

    10.2 Using the Custom Test Generator

    If you want to generate tests for languages other than Java or you are not happy with the way the verifier builds Java tests, you can register your own implementation.

    The SingleTestGenerator interface lets you register your own implementation. The -following code listing shows the SingleTestGenerator interface:

    package org.springframework.cloud.contract.verifier.builder
    -
    -import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
    -import org.springframework.cloud.contract.verifier.file.ContractMetadata
    -/**
    - * Builds a single test.
    - *
    - * @since 1.1.0
    - */
    -interface SingleTestGenerator {
    +following code listing shows the SingleTestGenerator interface:

    	 *
    +	 * @param properties                    - properties passed to the plugin
    +	 * @param listOfFiles                   - list of parsed contracts with additional metadata
    +	 * @param className                     - the name of the generated test class
    +	 * @param classPackage                  - the name of the package in which the test class should be stored
    +	 * @param includedDirectoryRelativePath - relative path to the included directory
    +	 * @return contents of a single test class
    +	 * @deprecated use {@link SingleTestGenerator#buildClass(ContractVerifierConfigProperties, Collection, String, GeneratedClassData)}
    +	 */
    +	@Deprecated
    +	String buildClass(ContractVerifierConfigProperties properties,
    +			Collection<ContractMetadata> listOfFiles, String className, String classPackage, String includedDirectoryRelativePath);
     
     	/**
     	 * Creates contents of a single test class in which all test scenarios from
     	 * the contract metadata should be placed.
     	 *
    -	 * @param properties - properties passed to the plugin
    -	 * @param listOfFiles - list of parsed contracts with additional metadata
    -	 * @param className - the name of the generated test class
    -	 * @param classPackage - the name of the package in which the test class should be stored
    +	 * @param properties                    - properties passed to the plugin
    +	 * @param listOfFiles                   - list of parsed contracts with additional metadata
    +	 * @param generatedClassData            - information about the generated class
     	 * @param includedDirectoryRelativePath - relative path to the included directory
     	 * @return contents of a single test class
     	 */
    -	String buildClass(ContractVerifierConfigProperties properties, Collection<ContractMetadata> listOfFiles,
    -					  String className, String classPackage, String includedDirectoryRelativePath)
    +	default String buildClass(ContractVerifierConfigProperties properties,
    +			Collection<ContractMetadata> listOfFiles, String includedDirectoryRelativePath, GeneratedClassData generatedClassData) {
    +		return buildClass(properties, listOfFiles, generatedClassData.className, generatedClassData.classPackage, includedDirectoryRelativePath);
    +	}
     
     	/**
     	 * Extension that should be appended to the generated test class. E.g. {@code .java} or {@code .php}
     	 *
     	 * @param properties - properties passed to the plugin
     	 */
    -	String fileExtension(ContractVerifierConfigProperties properties)
    +	String fileExtension(ContractVerifierConfigProperties properties);
    +
    +	class GeneratedClassData {
    +		public final String className;
    +		public final String classPackage;
    +		public final java.nio.file.Path testClassPath;
    +
    +		public GeneratedClassData(String className, String classPackage,
    +				java.nio.file.Path testClassPath) {
    +			this.className = className;
    +			this.classPackage = classPackage;
    +			this.testClassPath = testClassPath;
    +		}
    +	}
     }

    Again, you must provide a spring.factories file, such as the one shown in the following example:

    org.springframework.cloud.contract.verifier.builder.SingleTestGenerator=/
     com.example.MyGenerator

    10.3 Using the Custom Stub Generator

    If you want to generate stubs for stub servers other than WireMock, you can plug in your @@ -6070,13 +6123,13 @@ to clone the repository and use it as a source of contracts to generate tests or stubs.

    Either via environment variables, system properties, properties set inside the plugin or contracts repository configuration you can tweak the downloader’s behaviour. Below you can find the list of -properties

    Table 10.1. SCM Stub Downloader properties

    Type of a property

    Name of the property

    Description

    * git.branch (plugin prop)

    * stubrunner.properties.git.branch (system prop)

    * STUBRUNNER_PROPERTIES_GIT_BRANCH (env prop)

    master

    Which branch to checkout

    * git.username (plugin prop)

    * stubrunner.properties.git.username (system prop)

    * STUBRUNNER_PROPERTIES_GIT_USERNAME (env prop)

     

    Git clone username

    * git.password (plugin prop)

    * stubrunner.properties.git.password (system prop)

    * STUBRUNNER_PROPERTIES_GIT_PASSWORD (env prop)

     

    Git clone password

    * git.no-of-attempts (plugin prop)

    * stubrunner.properties.git.no-of-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_NO_OF_ATTEMPTS (env prop)

    10

    Number of attempts to push the commits to origin

    * git.wait-between-attempts (Plugin prop)

    * stubrunner.properties.git.wait-between-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_WAIT_BETWEEN_ATTEMPTS (env prop)

    1000

    Number of millis to wait between attempts to push the commits to origin


    10.7 Using the Pact Stub Downloader

    Whenever the repositoryRoot starts with a Pact protocol +properties

    Table 10.1. SCM Stub Downloader properties

    Type of a property

    Name of the property

    Description

    * git.branch (plugin prop)

    * stubrunner.properties.git.branch (system prop)

    * STUBRUNNER_PROPERTIES_GIT_BRANCH (env prop)

    master

    Which branch to checkout

    * git.username (plugin prop)

    * stubrunner.properties.git.username (system prop)

    * STUBRUNNER_PROPERTIES_GIT_USERNAME (env prop)

     

    Git clone username

    * git.password (plugin prop)

    * stubrunner.properties.git.password (system prop)

    * STUBRUNNER_PROPERTIES_GIT_PASSWORD (env prop)

     

    Git clone password

    * git.no-of-attempts (plugin prop)

    * stubrunner.properties.git.no-of-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_NO_OF_ATTEMPTS (env prop)

    10

    Number of attempts to push the commits to origin

    * git.wait-between-attempts (Plugin prop)

    * stubrunner.properties.git.wait-between-attempts (system prop)

    * STUBRUNNER_PROPERTIES_GIT_WAIT_BETWEEN_ATTEMPTS (env prop)

    1000

    Number of millis to wait between attempts to push the commits to origin


    10.7 Using the Pact Stub Downloader

    Whenever the repositoryRoot starts with a Pact protocol (starts with pact://), the stub downloader will try to fetch the Pact contract definitions from the Pact Broker. Whatever is set after pact:// will be parsed as the Pact Broker URL.

    Either via environment variables, system properties, properties set inside the plugin or contracts repository configuration you can tweak the downloader’s behaviour. Below you can find the list of -properties

    Table 10.2. SCM Stub Downloader properties

    Name of a property

    Default

    Description

    * pactbroker.host (plugin prop)

    * stubrunner.properties.pactbroker.host (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_HOST (env prop)

    Host from URL passed to repositoryRoot

    What is the URL of Pact Broker

    * pactbroker.port (plugin prop)

    * stubrunner.properties.pactbroker.port (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PORT (env prop)

    Port from URL passed to repositoryRoot

    What is the port of Pact Broker

    * pactbroker.protocol (plugin prop)

    * stubrunner.properties.pactbroker.protocol (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROTOCOL (env prop)

    Protocol from URL passed to repositoryRoot

    What is the protocol of Pact Broker

    * pactbroker.tags (plugin prop)

    * stubrunner.properties.pactbroker.tags (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_TAGS (env prop)

    Version of the stub, or latest if version is +

    What tags should be used to fetch the stub

    * pactbroker.auth.scheme (plugin prop)

    * stubrunner.properties.pactbroker.auth.scheme (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_SCHEME (env prop)

    Basic

    What kind of authentication should be used to connect to the Pact Broker

    * pactbroker.auth.username (plugin prop)

    * stubrunner.properties.pactbroker.auth.username (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_USERNAME (env prop)

    The username passed to contractsRepositoryUsername (maven) or contractRepository.username (gradle)

    Username used to connect to the Pact Broker

    * pactbroker.auth.password (plugin prop)

    * stubrunner.properties.pactbroker.auth.password (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_PASSWORD (env prop)

    The password passed to contractsRepositoryPassword (maven) or contractRepository.password (gradle)

    Password used to connect to the Pact Broker

    * pactbroker.provider-name-with-group-id (plugin prop)

    * stubrunner.properties.pactbroker.provider-name-with-group-id (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROVIDER_NAME_WITH_GROUP_ID (env prop)

    false

    When true, the provider name will be a combination of groupId:artifactId. If false, just artifactId is used


    11. Spring Cloud Contract WireMock

    The Spring Cloud Contract WireMock modules let you use WireMock in a +properties

    Table 10.2. SCM Stub Downloader properties

    Name of a property

    Default

    Description

    * pactbroker.host (plugin prop)

    * stubrunner.properties.pactbroker.host (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_HOST (env prop)

    Host from URL passed to repositoryRoot

    What is the URL of Pact Broker

    * pactbroker.port (plugin prop)

    * stubrunner.properties.pactbroker.port (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PORT (env prop)

    Port from URL passed to repositoryRoot

    What is the port of Pact Broker

    * pactbroker.protocol (plugin prop)

    * stubrunner.properties.pactbroker.protocol (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROTOCOL (env prop)

    Protocol from URL passed to repositoryRoot

    What is the protocol of Pact Broker

    * pactbroker.tags (plugin prop)

    * stubrunner.properties.pactbroker.tags (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_TAGS (env prop)

    Version of the stub, or latest if version is +

    What tags should be used to fetch the stub

    * pactbroker.auth.scheme (plugin prop)

    * stubrunner.properties.pactbroker.auth.scheme (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_SCHEME (env prop)

    Basic

    What kind of authentication should be used to connect to the Pact Broker

    * pactbroker.auth.username (plugin prop)

    * stubrunner.properties.pactbroker.auth.username (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_USERNAME (env prop)

    The username passed to contractsRepositoryUsername (maven) or contractRepository.username (gradle)

    Username used to connect to the Pact Broker

    * pactbroker.auth.password (plugin prop)

    * stubrunner.properties.pactbroker.auth.password (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_AUTH_PASSWORD (env prop)

    The password passed to contractsRepositoryPassword (maven) or contractRepository.password (gradle)

    Password used to connect to the Pact Broker

    * pactbroker.provider-name-with-group-id (plugin prop)

    * stubrunner.properties.pactbroker.provider-name-with-group-id (system prop)

    * STUBRUNNER_PROPERTIES_PACTBROKER_PROVIDER_NAME_WITH_GROUP_ID (env prop)

    false

    When true, the provider name will be a combination of groupId:artifactId. If false, just artifactId is used


    11. Spring Cloud Contract WireMock

    The Spring Cloud Contract WireMock modules let you use WireMock in a Spring Boot application. Check out the samples for more details.

    If you have a Spring Boot application that uses Tomcat as an embedded server (which is diff --git a/spring-cloud-contract-maven-plugin/checkstyle.html b/spring-cloud-contract-maven-plugin/checkstyle.html index c2898bbe57..ffca71d5ce 100644 --- a/spring-cloud-contract-maven-plugin/checkstyle.html +++ b/spring-cloud-contract-maven-plugin/checkstyle.html @@ -370,7 +370,7 @@ RightCurly -12 +11  Error coding @@ -380,7 +380,7 @@ HiddenField -34 +33  Error @@ -400,7 +400,7 @@ javadoc JavadocMethod -55 +54  Error @@ -420,12 +420,12 @@ JavadocVariable -69 +70  Error misc FinalParameters -55 +54  Error naming @@ -435,7 +435,7 @@ sizes LineLength -231 +234  Error @@ -1740,264 +1740,264 @@ javadoc JavadocStyle First sentence should end with a period. -44 +45  Error sizes LineLength Line is longer than 80 characters (found 83). -45 +46  Error sizes LineLength Line is longer than 80 characters (found 135). -48 +49  Error whitespace FileTabCharacter File contains tab characters (this is the first instance). -51 +52  Error javadoc JavadocVariable Missing a Javadoc comment. -51 +52  Error sizes LineLength Line is longer than 80 characters (found 148). -54 +55  Error javadoc JavadocVariable Missing a Javadoc comment. -54 +55  Error sizes LineLength Line is longer than 80 characters (found 96). -57 +58  Error javadoc JavadocVariable Missing a Javadoc comment. -57 +58 + + Error +sizes +LineLength +Line is longer than 80 characters (found 98). +61 + + Error +javadoc +JavadocVariable +Missing a Javadoc comment. +61  Error javadoc JavadocVariable Missing a Javadoc comment. -60 +64  Error javadoc JavadocVariable Missing a Javadoc comment. -63 +67  Error javadoc JavadocVariable Missing a Javadoc comment. -66 +70  Error javadoc JavadocVariable Missing a Javadoc comment. -69 +73  Error javadoc JavadocVariable Missing a Javadoc comment. -72 +76  Error javadoc JavadocVariable Missing a Javadoc comment. -75 +79  Error javadoc JavadocStyle First sentence should end with a period. -78 +82  Error javadoc JavadocStyle First sentence should end with a period. -84 +88  Error javadoc JavadocStyle First sentence should end with a period. -90 +94  Error javadoc JavadocStyle First sentence should end with a period. -96 +100  Error sizes LineLength Line is longer than 80 characters (found 86). -103 +107  Error sizes LineLength Line is longer than 80 characters (found 99). -106 +110  Error javadoc JavadocStyle First sentence should end with a period. -109 +113  Error sizes LineLength Line is longer than 80 characters (found 91). -110 +114  Error javadoc JavadocVariable Missing a Javadoc comment. -115 +119  Error sizes LineLength Line is longer than 80 characters (found 92). -118 +122  Error javadoc JavadocVariable Missing a Javadoc comment. -118 +122  Error javadoc JavadocVariable Missing a Javadoc comment. -121 +125  Error javadoc JavadocVariable Missing a Javadoc comment. -124 +128  Error sizes LineLength Line is longer than 80 characters (found 84). -128 +132  Error sizes LineLength Line is longer than 80 characters (found 88). -129 +133  Error javadoc JavadocVariable Missing a Javadoc comment. -135 +139  Error sizes LineLength Line is longer than 80 characters (found 89). -139 +143  Error sizes LineLength Line is longer than 80 characters (found 87). -140 +144  Error javadoc JavadocStyle Extra HTML tag found: </p> -142 +146  Error sizes LineLength Line is longer than 80 characters (found 94). -143 +147  Error javadoc JavadocStyle First sentence should end with a period. -149 +153  Error sizes LineLength Line is longer than 80 characters (found 92). -156 +160  Error sizes LineLength Line is longer than 80 characters (found 93). -157 - - Error -sizes -LineLength -Line is longer than 80 characters (found 93). -159 - - Error -sizes -LineLength -Line is longer than 80 characters (found 89). 161  Error sizes LineLength Line is longer than 80 characters (found 93). -168 +163  Error sizes LineLength Line is longer than 80 characters (found 89). -169 +165  Error -javadoc -JavadocStyle -Extra HTML tag found: </p> -171 +sizes +LineLength +Line is longer than 80 characters (found 93). +172  Error -javadoc -JavadocStyle -Extra HTML tag found: </p> +sizes +LineLength +Line is longer than 80 characters (found 89). 173  Error @@ -2007,111 +2007,111 @@ 175  Error +javadoc +JavadocStyle +Extra HTML tag found: </p> +177 + + Error +javadoc +JavadocStyle +Extra HTML tag found: </p> +179 + + Error sizes LineLength Line is longer than 80 characters (found 91). -176 +180  Error javadoc JavadocStyle First sentence should end with a period. -207 +211  Error sizes LineLength Line is longer than 80 characters (found 92). -208 +212  Error sizes LineLength Line is longer than 80 characters (found 83). -212 +216  Error javadoc JavadocStyle First sentence should end with a period. -216 +220  Error sizes LineLength Line is longer than 80 characters (found 94). -217 +221  Error javadoc JavadocStyle First sentence should end with a period. -223 +227  Error sizes LineLength Line is longer than 80 characters (found 86). -225 +229  Error javadoc JavadocVariable Missing a Javadoc comment. -230 +234  Error javadoc JavadocMethod Missing a Javadoc comment. -232 +236  Error sizes LineLength Line is longer than 80 characters (found 91). -233 +237  Error misc FinalParameters Parameter aetherStubDownloaderFactory should be final. -233 +237  Error coding HiddenField 'aetherStubDownloaderFactory' hides a field. -233 +237  Error sizes LineLength Line is longer than 80 characters (found 83). -237 +241  Error design DesignForExtension Class 'GenerateTestsMojo' looks like designed for extension (can be subclassed), but the method 'execute' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'GenerateTestsMojo' final or making the method 'execute' static/final/abstract/empty, or adding allowed annotation for the method. -237 +241  Error javadoc JavadocMethod Missing a Javadoc comment. -237 - - Error -blocks -NeedBraces -'if' construct must use '{}'s. -239 - - Error -sizes -LineLength -Line is longer than 80 characters (found 137). 241  Error @@ -2123,188 +2123,188 @@  Error sizes LineLength -Line is longer than 80 characters (found 117). +Line is longer than 80 characters (found 137). 245  Error -sizes -LineLength -Line is longer than 80 characters (found 86). -246 - - Error blocks NeedBraces 'if' construct must use '{}'s. 247 + + Error +sizes +LineLength +Line is longer than 80 characters (found 117). +249 + + Error +sizes +LineLength +Line is longer than 80 characters (found 86). +250 + + Error +blocks +NeedBraces +'if' construct must use '{}'s. +251  Error sizes LineLength Line is longer than 80 characters (found 110). -249 +253  Error sizes LineLength Line is longer than 80 characters (found 82). -250 +254  Error sizes LineLength Line is longer than 80 characters (found 128). -254 - - Error -sizes -LineLength -Line is longer than 80 characters (found 103). -255 - - Error -sizes -LineLength -Line is longer than 80 characters (found 84). -257 - - Error -coding -HiddenField -'contractsDirectory' hides a field. -257 - - Error -sizes -LineLength -Line is longer than 80 characters (found 105). 258  Error sizes LineLength -Line is longer than 80 characters (found 95). +Line is longer than 80 characters (found 103). 259  Error sizes LineLength -Line is longer than 80 characters (found 100). -260 +Line is longer than 80 characters (found 84). +261  Error -sizes -LineLength -Line is longer than 80 characters (found 93). +coding +HiddenField +'contractsDirectory' hides a field. 261  Error sizes LineLength -Line is longer than 80 characters (found 102). +Line is longer than 80 characters (found 105). 262  Error sizes LineLength -Line is longer than 80 characters (found 102). +Line is longer than 80 characters (found 95). +263 + + Error +sizes +LineLength +Line is longer than 80 characters (found 100). +264 + + Error +sizes +LineLength +Line is longer than 80 characters (found 93). 265  Error sizes LineLength -Line is longer than 80 characters (found 106). -268 +Line is longer than 80 characters (found 102). +266  Error sizes LineLength Line is longer than 80 characters (found 102). -271 +269  Error sizes LineLength +Line is longer than 80 characters (found 106). +272 + + Error +sizes +LineLength Line is longer than 80 characters (found 88). 274 - - Error -sizes -LineLength -Line is longer than 80 characters (found 101). -275  Error sizes LineLength -Line is longer than 80 characters (found 101). -276 +Line is longer than 80 characters (found 102). +279  Error sizes LineLength -Line is longer than 80 characters (found 90). +Line is longer than 80 characters (found 88). 282  Error -blocks -RightCurly -'}' at column 3 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally). +sizes +LineLength +Line is longer than 80 characters (found 101). 283  Error sizes LineLength -Line is longer than 80 characters (found 108). -286 +Line is longer than 80 characters (found 101). +284  Error -javadoc -JavadocMethod -Missing a Javadoc comment. -292 - - Error -misc -FinalParameters -Parameter config should be final. -292 - - Error -misc -FinalParameters -Parameter contractsDirectory should be final. -293 - - Error -coding -HiddenField -'contractsDirectory' hides a field. -293 - - Error -javadoc -JavadocMethod -Missing a Javadoc comment. -314 - - Error -misc -FinalParameters -Parameter contractsDirectory should be final. -314 - - Error -coding -HiddenField -'contractsDirectory' hides a field. -314 +sizes +LineLength +Line is longer than 80 characters (found 90). +290  Error blocks RightCurly '}' at column 3 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally). -317 +291 + + Error +sizes +LineLength +Line is longer than 80 characters (found 108). +294 + + Error +javadoc +JavadocMethod +Missing a Javadoc comment. +300 + + Error +misc +FinalParameters +Parameter config should be final. +300 + + Error +misc +FinalParameters +Parameter contractsDirectory should be final. +301 + + Error +coding +HiddenField +'contractsDirectory' hides a field. +301 + + Error +sizes +LineLength +Line is longer than 80 characters (found 84). +304  Error design diff --git a/spring-cloud-contract-maven-plugin/generateTests-mojo.html b/spring-cloud-contract-maven-plugin/generateTests-mojo.html index c1bb69c911..7b2593fc4c 100644 --- a/spring-cloud-contract-maven-plugin/generateTests-mojo.html +++ b/spring-cloud-contract-maven-plugin/generateTests-mojo.html @@ -509,6 +509,17 @@ folder after running tests
    Default value is: true.
    +generatedTestResourcesDir + +File + +- + +(no description)
    Default value is: ${project.build.directory}/generated-test-resources/contracts.
    + + + + generatedTestSourcesDir File @@ -518,7 +529,7 @@ folder after running tests
    Default value is: true.
    (no description)
    Default value is: ${project.build.directory}/generated-test-sources/contracts.
    - + ignoredFiles @@ -530,7 +541,7 @@ folder after running tests
    Default value is: true.
    @Ignored tests
    - + imports @@ -541,7 +552,7 @@ folder after running tests
    Default value is: true.
    Imports that should be added to generated tests
    - + includedFiles @@ -552,7 +563,7 @@ folder after running tests
    Default value is: true.
    Patterns that should be taken into account for processing
    User property is: includedFiles.
    - + mavenTestSkip @@ -563,7 +574,7 @@ folder after running tests
    Default value is: true.
    (no description)
    Default value is: false.
    User property is: maven.test.skip.
    - + nameSuffixForTests @@ -574,7 +585,7 @@ folder after running tests
    Default value is: true.
    (no description)
    - + packageWithBaseClasses @@ -593,7 +604,7 @@ test source file that will have the package last folders to and attach Base to its name.
    User property is: packageWithBaseClasses.
    - + ruleClassForTests @@ -604,7 +615,7 @@ last folders to and attach Base to its name.
    User property is(no description)
    - + skip @@ -615,7 +626,7 @@ last folders to and attach Base to its name.
    User property is(no description)
    Default value is: false.
    User property is: spring.cloud.contract.verifier.skip.
    - + skipTests @@ -626,7 +637,7 @@ last folders to and attach Base to its name.
    User property is(no description)
    Default value is: false.
    User property is: skipTests.
    - + staticImports @@ -637,7 +648,7 @@ last folders to and attach Base to its name.
    User property isStatic imports that should be added to generated tests
    - + testFramework @@ -648,7 +659,7 @@ last folders to and attach Base to its name.
    User property is(no description)
    Default value is: JUNIT.
    - + testMode @@ -898,6 +909,18 @@ folder after running tests

  • Required: No

  • +

    generatedTestResourcesDir:

    + +
    (no description)
    + +
      + +
    • Type: java.io.File
    • + +
    • Required: No
    • + +
    • Default: ${project.build.directory}/generated-test-resources/contracts
    • +

    generatedTestSourcesDir:

    (no description)
    diff --git a/spring-cloud-contract.xml b/spring-cloud-contract.xml index 6f5b7f24bc..f0676967d1 100644 --- a/spring-cloud-contract.xml +++ b/spring-cloud-contract.xml @@ -684,7 +684,7 @@ You would like to feed that instance with a proper stub definition. FraudServiceResponse.class); Annotate your test class with @AutoConfigureStubRunner. In the annotation provide the group id and artifact id for the Stub Runner to download stubs of your collaborators. @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment=WebEnvironment.NONE) +@SpringBootTest(webEnvironment = WebEnvironment.NONE) @AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:6565"}, stubsMode = StubRunnerProperties.StubsMode.LOCAL) public class LoanApplicationServiceTests { @@ -1142,7 +1142,7 @@ Application service): collaborators. (Optional step) Because you’re playing with the collaborators offline, you can also provide the offline work switch (StubRunnerProperties.StubsMode.LOCAL). @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment=WebEnvironment.NONE) +@SpringBootTest(webEnvironment = WebEnvironment.NONE) @AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:6565"}, stubsMode = StubRunnerProperties.StubsMode.LOCAL) public class LoanApplicationServiceTests { @@ -1205,9 +1205,8 @@ or whatever is necessary. In this case, use package com.example.fraud; -import org.junit.Before; - import io.restassured.module.mockmvc.RestAssuredMockMvc; +import org.junit.Before; public class FraudBase { @Before @@ -2501,6 +2500,7 @@ pseudocode): testFramework ='JUNIT' testMode = 'MockMvc' generatedTestSourcesDir = project.file("${project.buildDir}/generated-test-sources/contracts") + generatedTestResourcesDir = project.file("${project.buildDir}/generated-test-resources/contracts") contractsDslDir = "${project.rootDir}/src/test/resources/contracts" basePackageForTests = 'org.springframework.cloud.verifier.tests' stubsOutputDir = project.file("${project.buildDir}/stubs") @@ -2602,7 +2602,12 @@ GroovyDSL. By default, its value is $rootDir/src/test/resources/contrac generatedTestSourcesDir: Specifies the test source directory where tests generated from the Groovy DSL should be placed. By default its value is -$buildDir/generated-test-sources/contractVerifier. +$buildDir/generated-test-sources/contracts. + + +generatedTestResourcesDir: Specifies the test resource directory where resources used by the tests generated +from the Groovy DSL should be placed. By default its value is +$buildDir/generated-test-resources/contracts. stubsOutputDir: Specifies the directory where the generated WireMock stubs from @@ -3013,6 +3018,14 @@ use Spock classes, the class is spock.lang.Specification./src/test/resources/contracts. +generatedTestSourcesDir: Specifies the test source directory where tests generated +from the Groovy DSL should be placed. By default its value is +$buildDir/generated-test-sources/contracts. + + +generatedTestResourcesDir: Specifies the test resource directory where resources used by the tests generated + + testFramework: Specifies the target test framework to be used. Currently, Spock, JUnit 4 (TestFramework.JUNIT) and JUnit 5 are supported with JUnit 4 being the default framework. @@ -6230,6 +6243,51 @@ response: When test or stub generation takes place, the contents of the file is passed to the body of a request or a response. The name of the file needs to be a file with location relative to the folder in which the contract lays. +If you need to pass the contents of a file in a binary form +it’s enough for you to use the fileAsBytes method in Groovy DSL or bodyFromFileAsBytes field in YAML. + +Groovy DSL + +import org.springframework.cloud.contract.spec.Contract + +Contract.make { + request { + url("/1") + method(PUT()) + headers { + contentType(applicationOctetStream()) + } + body(fileAsBytes("request.pdf")) + } + response { + status 200 + body(fileAsBytes("response.pdf")) + headers { + contentType(applicationOctetStream()) + } + } +} + + + +YAML + +request: + url: /1 + method: PUT + headers: + Content-Type: application/octet-stream + bodyFromFileAsBytes: request.pdf +response: + status: 200 + bodyFromFileAsBytes: response.pdf + headers: + Content-Type: application/octet-stream + + + +You should use this approach whenever you want to work with binary payloads both for HTTP and messaging. +
    HTTP Top-Level Elements @@ -7174,16 +7232,16 @@ is applied for the whole body - not for parts of it. The following example shows how to read an object from JSON: Contract contractDsl = Contract.make { - request { - method 'GET' - url '/something' - body( - $(c('foo'), p(execute('hashCode()'))) - ) - } - response { - status OK() - } + request { + method 'GET' + url '/something' + body( + $(c('foo'), p(execute('hashCode()'))) + ) + } + response { + status OK() + } } The preceding example results in calling the hashCode() method in the request body. It should resemble the following code: @@ -9290,37 +9348,53 @@ current Pact version that you use. way the verifier builds Java tests, you can register your own implementation. The SingleTestGenerator interface lets you register your own implementation. The following code listing shows the SingleTestGenerator interface: -package org.springframework.cloud.contract.verifier.builder - -import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties -import org.springframework.cloud.contract.verifier.file.ContractMetadata -/** - * Builds a single test. - * - * @since 1.1.0 - */ -interface SingleTestGenerator { + * + * @param properties - properties passed to the plugin + * @param listOfFiles - list of parsed contracts with additional metadata + * @param className - the name of the generated test class + * @param classPackage - the name of the package in which the test class should be stored + * @param includedDirectoryRelativePath - relative path to the included directory + * @return contents of a single test class + * @deprecated use {@link SingleTestGenerator#buildClass(ContractVerifierConfigProperties, Collection, String, GeneratedClassData)} + */ + @Deprecated + String buildClass(ContractVerifierConfigProperties properties, + Collection<ContractMetadata> listOfFiles, String className, String classPackage, String includedDirectoryRelativePath); /** * Creates contents of a single test class in which all test scenarios from * the contract metadata should be placed. * - * @param properties - properties passed to the plugin - * @param listOfFiles - list of parsed contracts with additional metadata - * @param className - the name of the generated test class - * @param classPackage - the name of the package in which the test class should be stored + * @param properties - properties passed to the plugin + * @param listOfFiles - list of parsed contracts with additional metadata + * @param generatedClassData - information about the generated class * @param includedDirectoryRelativePath - relative path to the included directory * @return contents of a single test class */ - String buildClass(ContractVerifierConfigProperties properties, Collection<ContractMetadata> listOfFiles, - String className, String classPackage, String includedDirectoryRelativePath) + default String buildClass(ContractVerifierConfigProperties properties, + Collection<ContractMetadata> listOfFiles, String includedDirectoryRelativePath, GeneratedClassData generatedClassData) { + return buildClass(properties, listOfFiles, generatedClassData.className, generatedClassData.classPackage, includedDirectoryRelativePath); + } /** * Extension that should be appended to the generated test class. E.g. {@code .java} or {@code .php} * * @param properties - properties passed to the plugin */ - String fileExtension(ContractVerifierConfigProperties properties) + String fileExtension(ContractVerifierConfigProperties properties); + + class GeneratedClassData { + public final String className; + public final String classPackage; + public final java.nio.file.Path testClassPath; + + public GeneratedClassData(String className, String classPackage, + java.nio.file.Path testClassPath) { + this.className = className; + this.classPackage = classPackage; + this.testClassPath = testClassPath; + } + } } Again, you must provide a spring.factories file, such as the one shown in the following example: