From 21a09ceff0dc4ee6f887ca4c227a137a48dc550a Mon Sep 17 00:00:00 2001
From: buildmaster
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 @@ capitalizefraud. That gives you theRest Assured MVC to start the server sideFraudDetectionController.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"
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
SingleTestGeneratorinterface lets you register your own implementation. The -following code listing shows theSingleTestGeneratorinterface: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 theSingleTestGeneratorinterface:* + * @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.factoriesfile, such as the one shown in the following example:org.springframework.cloud.contract.verifier.builder.SingleTestGenerator=/ com.example.MyGeneratorIf 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
originWhenever the
repositoryRootstarts with a Pact protocol +propertiesTable 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
originWhenever the
repositoryRootstarts with a Pact protocol (starts withpact://), the stub downloader will try to fetch the Pact contract definitions from the Pact Broker. Whatever is set afterpact://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
repositoryRootWhat 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
repositoryRootWhat 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
repositoryRootWhat 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
latestif 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)
BasicWhat 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) orcontractRepository.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) orcontractRepository.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 ofgroupId:artifactId. Iffalse, justartifactIdis used