diff --git a/README.adoc b/README.adoc index a950a8fddf..39e4e41f5e 100644 --- a/README.adoc +++ b/README.adoc @@ -922,7 +922,7 @@ You have to add the dependencies needed by the autogenerated tests [source,xml,indent=0] ---- - + org.springframework.cloud spring-cloud-starter-contract-verifier test diff --git a/docs/src/main/asciidoc/verifier/rest.adoc b/docs/src/main/asciidoc/verifier/rest.adoc index cc32fe10f7..0354a72b8e 100644 --- a/docs/src/main/asciidoc/verifier/rest.adoc +++ b/docs/src/main/asciidoc/verifier/rest.adoc @@ -41,6 +41,36 @@ dependencies { } ---- +====== Gradle and Rest Assured 3.0 + +By default Rest Assured 2.x is added to the classpath. However in order to give the users the +opportunity to use Rest Assured 3.x it's enough to add it to the plugins classpath. + +[source,groovy,indent=0] +---- +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}" + classpath "io.rest-assured:rest-assured:3.0.2" + classpath "io.rest-assured:spring-mock-mvc:3.0.2" + } +} + +depenendencies { + // all dependencies + // you can exclude rest-assured from spring-cloud-contract-verifier + testCompile "io.rest-assured:rest-assured:3.0.2" + testCompile "io.rest-assured:spring-mock-mvc:3.0.2" +} +---- + +That way the plugin will automatically see that Rest Assured 3.x is present on the classpath +and will modify the imports accordingly. + ====== Snapshot versions for Gradle Add the additional snapshot repository to your build.gradle to use snapshot versions which are automatically uploaded after every successful build: @@ -277,6 +307,63 @@ include::{standalone_samples_path}/http-server/pom.xml[tags=contract_maven_plugi You can read more in the https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract-maven-plugin/[Spring Cloud Contract Maven Plugin Docs] +====== Maven and Rest Assured 3.0 + +By default Rest Assured 2.x is added to the classpath. However in order to give the users the +opportunity to use Rest Assured 3.x it's enough to add it to the plugins classpath. + +[source,groovy,indent=0] +---- + + org.springframework.cloud + spring-cloud-contract-maven-plugin + ${spring-cloud-contract.version} + true + + com.example + + + + org.springframework.cloud + spring-cloud-contract-verifier + ${spring-cloud-contract.version} + + + io.rest-assured + rest-assured + 3.0.2 + compile + + + io.rest-assured + spring-mock-mvc + 3.0.2 + compile + + + + + + + + + io.rest-assured + rest-assured + 3.0.2 + test + + + io.rest-assured + spring-mock-mvc + 3.0.2 + test + + +---- + +That way the plugin will automatically see that Rest Assured 3.x is present on the classpath +and will modify the imports accordingly. + ====== Snapshot versions for Maven For Snapshot / Milestone versions you have to add the following section to your `pom.xml` diff --git a/samples/standalone/contracts/com/example/server/pom.xml b/samples/standalone/contracts/com/example/server/pom.xml index c2286dc83e..f16d5bf8f3 100644 --- a/samples/standalone/contracts/com/example/server/pom.xml +++ b/samples/standalone/contracts/com/example/server/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT Dalston.BUILD-SNAPSHOT true diff --git a/samples/standalone/dsl/http-server/gradle.properties b/samples/standalone/dsl/http-server/gradle.properties index 38dc897f59..a892800057 100644 --- a/samples/standalone/dsl/http-server/gradle.properties +++ b/samples/standalone/dsl/http-server/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=false -verifierVersion=1.1.0.BUILD-SNAPSHOT +verifierVersion=1.1.1.BUILD-SNAPSHOT BOM_VERSION=Dalston.BUILD-SNAPSHOT \ No newline at end of file diff --git a/samples/standalone/dsl/http-server/pom.xml b/samples/standalone/dsl/http-server/pom.xml index 9363f7ebb5..e0a6490c58 100644 --- a/samples/standalone/dsl/http-server/pom.xml +++ b/samples/standalone/dsl/http-server/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT Dalston.BUILD-SNAPSHOT @@ -42,10 +42,10 @@ - org.springframework.cloud - spring-cloud-starter-contract-verifier - test - + org.springframework.cloud + spring-cloud-starter-contract-verifier + test + diff --git a/samples/standalone/dsl/pom.xml b/samples/standalone/dsl/pom.xml index 58a7f56790..13c73b511d 100644 --- a/samples/standalone/dsl/pom.xml +++ b/samples/standalone/dsl/pom.xml @@ -17,7 +17,7 @@ Spring Cloud Contract Standalone Test Samples used for end to end tests - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/standalone/messaging/pom.xml b/samples/standalone/messaging/pom.xml index a463b172a6..8893959be8 100644 --- a/samples/standalone/messaging/pom.xml +++ b/samples/standalone/messaging/pom.xml @@ -17,7 +17,7 @@ Spring Cloud Contract Standalone Test Samples used for end to end tests - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/standalone/messaging/stream-sink/gradle.properties b/samples/standalone/messaging/stream-sink/gradle.properties index 38dc897f59..a892800057 100644 --- a/samples/standalone/messaging/stream-sink/gradle.properties +++ b/samples/standalone/messaging/stream-sink/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=false -verifierVersion=1.1.0.BUILD-SNAPSHOT +verifierVersion=1.1.1.BUILD-SNAPSHOT BOM_VERSION=Dalston.BUILD-SNAPSHOT \ No newline at end of file diff --git a/samples/standalone/messaging/stream-sink/pom.xml b/samples/standalone/messaging/stream-sink/pom.xml index c778c1afd9..a0ddf7521b 100644 --- a/samples/standalone/messaging/stream-sink/pom.xml +++ b/samples/standalone/messaging/stream-sink/pom.xml @@ -21,7 +21,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/standalone/messaging/stream-source/build.gradle b/samples/standalone/messaging/stream-source/build.gradle index d3fd7cba38..7d03c87c30 100644 --- a/samples/standalone/messaging/stream-source/build.gradle +++ b/samples/standalone/messaging/stream-source/build.gradle @@ -9,6 +9,8 @@ buildscript { dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.0.BUILD-SNAPSHOT" classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:${findProperty('verifierVersion') ?: verifierVersion}" + classpath "io.rest-assured:rest-assured:3.0.2" + classpath "io.rest-assured:spring-mock-mvc:3.0.2" } } @@ -51,7 +53,9 @@ dependencies { compile("org.springframework.cloud:spring-cloud-starter-stream-rabbit") testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier' - testCompile("org.springframework.cloud:spring-cloud-stream-test-support") + testCompile "org.springframework.cloud:spring-cloud-stream-test-support" + testCompile "io.rest-assured:rest-assured:3.0.2" + testCompile "io.rest-assured:spring-mock-mvc:3.0.2" } task stubsJar(type: Jar, dependsOn: "generateWireMockClientStubs") { diff --git a/samples/standalone/messaging/stream-source/gradle.properties b/samples/standalone/messaging/stream-source/gradle.properties index 38dc897f59..a892800057 100644 --- a/samples/standalone/messaging/stream-source/gradle.properties +++ b/samples/standalone/messaging/stream-source/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=false -verifierVersion=1.1.0.BUILD-SNAPSHOT +verifierVersion=1.1.1.BUILD-SNAPSHOT BOM_VERSION=Dalston.BUILD-SNAPSHOT \ No newline at end of file diff --git a/samples/standalone/messaging/stream-source/pom.xml b/samples/standalone/messaging/stream-source/pom.xml index c41b6a874b..f11d74d600 100644 --- a/samples/standalone/messaging/stream-source/pom.xml +++ b/samples/standalone/messaging/stream-source/pom.xml @@ -21,7 +21,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT @@ -42,6 +42,31 @@ org.springframework.cloud spring-cloud-contract-verifier + ${spring-cloud-contract.version} + test + + + com.jayway.restassured + rest-assured + + + com.jayway.restassured + spring-mock-mvc + + + + + + + io.rest-assured + rest-assured + 3.0.2 + test + + + io.rest-assured + spring-mock-mvc + 3.0.2 test @@ -77,6 +102,22 @@ + + + + + io.rest-assured + rest-assured + 3.0.2 + compile + + + io.rest-assured + spring-mock-mvc + 3.0.2 + compile + + diff --git a/samples/standalone/messaging/stream-source/src/test/java/com/example/source/SensorSourceTestBase.java b/samples/standalone/messaging/stream-source/src/test/java/com/example/source/SensorSourceTestBase.java index fb775bbf83..63d504e5f8 100644 --- a/samples/standalone/messaging/stream-source/src/test/java/com/example/source/SensorSourceTestBase.java +++ b/samples/standalone/messaging/stream-source/src/test/java/com/example/source/SensorSourceTestBase.java @@ -16,11 +16,14 @@ package com.example.source; +import io.restassured.module.mockmvc.RestAssuredMockMvc; +import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.contract.verifier.messaging.boot.AutoConfigureMessageVerifier; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.context.WebApplicationContext; /** * Base class for sensor autogenerated tests (used by Spring Cloud Contract). @@ -37,6 +40,14 @@ public abstract class SensorSourceTestBase { @Autowired ContractVerifierSampleStreamSourceApplication application; + @Autowired + WebApplicationContext context; + + @Before + public void setup() { + RestAssuredMockMvc.webAppContextSetup(this.context); + } + public void createSensorData() { application.poll(); } diff --git a/samples/standalone/messaging/stream-source/src/test/resources/contracts/shouldReturnOkWhenServiceIsHealthy.groovy b/samples/standalone/messaging/stream-source/src/test/resources/contracts/shouldReturnOkWhenServiceIsHealthy.groovy new file mode 100644 index 0000000000..781257e6c2 --- /dev/null +++ b/samples/standalone/messaging/stream-source/src/test/resources/contracts/shouldReturnOkWhenServiceIsHealthy.groovy @@ -0,0 +1,11 @@ +package contracts + +org.springframework.cloud.contract.spec.Contract.make { + request { + method(GET()) + url("/health") + } + response { + status(200) + } +} diff --git a/samples/standalone/pact/pact-http-server/gradle.properties b/samples/standalone/pact/pact-http-server/gradle.properties index 38dc897f59..a892800057 100644 --- a/samples/standalone/pact/pact-http-server/gradle.properties +++ b/samples/standalone/pact/pact-http-server/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=false -verifierVersion=1.1.0.BUILD-SNAPSHOT +verifierVersion=1.1.1.BUILD-SNAPSHOT BOM_VERSION=Dalston.BUILD-SNAPSHOT \ No newline at end of file diff --git a/samples/standalone/pact/pact-http-server/pom.xml b/samples/standalone/pact/pact-http-server/pom.xml index 26ead7abd4..310125e090 100644 --- a/samples/standalone/pact/pact-http-server/pom.xml +++ b/samples/standalone/pact/pact-http-server/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT Dalston.BUILD-SNAPSHOT diff --git a/samples/standalone/pact/pom.xml b/samples/standalone/pact/pom.xml index 16a1c71e7e..3b582cc2b7 100644 --- a/samples/standalone/pact/pom.xml +++ b/samples/standalone/pact/pom.xml @@ -17,7 +17,7 @@ Spring Cloud Contract Standalone Test Samples used for end to end tests with Pact - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/standalone/restdocs/http-client/gradle.properties b/samples/standalone/restdocs/http-client/gradle.properties index 38dc897f59..a892800057 100644 --- a/samples/standalone/restdocs/http-client/gradle.properties +++ b/samples/standalone/restdocs/http-client/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=false -verifierVersion=1.1.0.BUILD-SNAPSHOT +verifierVersion=1.1.1.BUILD-SNAPSHOT BOM_VERSION=Dalston.BUILD-SNAPSHOT \ No newline at end of file diff --git a/samples/standalone/restdocs/http-client/pom.xml b/samples/standalone/restdocs/http-client/pom.xml index 21959f11be..4e74d158d8 100644 --- a/samples/standalone/restdocs/http-client/pom.xml +++ b/samples/standalone/restdocs/http-client/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT 1.1.8.RELEASE diff --git a/samples/standalone/restdocs/http-server/gradle.properties b/samples/standalone/restdocs/http-server/gradle.properties index 38dc897f59..a892800057 100644 --- a/samples/standalone/restdocs/http-server/gradle.properties +++ b/samples/standalone/restdocs/http-server/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=false -verifierVersion=1.1.0.BUILD-SNAPSHOT +verifierVersion=1.1.1.BUILD-SNAPSHOT BOM_VERSION=Dalston.BUILD-SNAPSHOT \ No newline at end of file diff --git a/samples/standalone/restdocs/http-server/pom.xml b/samples/standalone/restdocs/http-server/pom.xml index 53f5711cef..92cb252004 100644 --- a/samples/standalone/restdocs/http-server/pom.xml +++ b/samples/standalone/restdocs/http-server/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/standalone/restdocs/pom.xml b/samples/standalone/restdocs/pom.xml index fecaae2a0c..45d64b19f8 100644 --- a/samples/standalone/restdocs/pom.xml +++ b/samples/standalone/restdocs/pom.xml @@ -17,7 +17,7 @@ Spring Cloud Contract Standalone Test Samples used for end to end tests - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/wiremock-jetty/pom.xml b/samples/wiremock-jetty/pom.xml index 8db1accaa2..187b4e9c2a 100644 --- a/samples/wiremock-jetty/pom.xml +++ b/samples/wiremock-jetty/pom.xml @@ -21,7 +21,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/wiremock-native/pom.xml b/samples/wiremock-native/pom.xml index 3e49cd6c98..5c6d7886ed 100644 --- a/samples/wiremock-native/pom.xml +++ b/samples/wiremock-native/pom.xml @@ -22,7 +22,7 @@ UTF-8 1.8 9.2.13.v20150730 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/wiremock-tomcat/pom.xml b/samples/wiremock-tomcat/pom.xml index 12749b2e60..4ec67858bd 100644 --- a/samples/wiremock-tomcat/pom.xml +++ b/samples/wiremock-tomcat/pom.xml @@ -21,7 +21,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/wiremock-undertow-ssl/pom.xml b/samples/wiremock-undertow-ssl/pom.xml index 76cd1e5bf7..12e0612d07 100644 --- a/samples/wiremock-undertow-ssl/pom.xml +++ b/samples/wiremock-undertow-ssl/pom.xml @@ -21,7 +21,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/samples/wiremock-undertow/pom.xml b/samples/wiremock-undertow/pom.xml index 45834afbb7..3d2b843cb9 100644 --- a/samples/wiremock-undertow/pom.xml +++ b/samples/wiremock-undertow/pom.xml @@ -21,7 +21,7 @@ UTF-8 1.8 - 1.1.0.BUILD-SNAPSHOT + 1.1.1.BUILD-SNAPSHOT diff --git a/spring-cloud-contract-dependencies/pom.xml b/spring-cloud-contract-dependencies/pom.xml index 3fc7abfea0..ea304b54f1 100644 --- a/spring-cloud-contract-dependencies/pom.xml +++ b/spring-cloud-contract-dependencies/pom.xml @@ -119,6 +119,7 @@ jsonassert ${jsonassert.version} + com.jayway.restassured spring-mock-mvc diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy index e3a34c2bbc..d6c25aeec5 100755 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/TestGenerator.groovy @@ -73,6 +73,9 @@ class TestGenerator { } int generate() { + if (!configProperties.basePackageForTests) { + + } generateTestClasses(configProperties.basePackageForTests) return counter.get() } diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JavaTestGenerator.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JavaTestGenerator.groovy index 6ab0756926..1db9c9b833 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JavaTestGenerator.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JavaTestGenerator.groovy @@ -18,25 +18,32 @@ package org.springframework.cloud.contract.verifier.builder import groovy.transform.Canonical import groovy.transform.EqualsAndHashCode -import groovy.util.logging.Slf4j +import groovy.transform.PackageScope +import org.apache.commons.logging.Log +import org.apache.commons.logging.LogFactory import org.springframework.cloud.contract.spec.Contract import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties import org.springframework.cloud.contract.verifier.config.TestFramework import org.springframework.cloud.contract.verifier.config.TestMode import org.springframework.cloud.contract.verifier.file.ContractMetadata -import static org.springframework.cloud.contract.verifier.util.NamesUtil.capitalize +import java.lang.invoke.MethodHandles +import static org.springframework.cloud.contract.verifier.util.NamesUtil.capitalize /** * Builds a single test for the given {@link ContractVerifierConfigProperties properties} * * @since 1.1.0 */ -@Slf4j class JavaTestGenerator implements SingleTestGenerator { + private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass()) + private static final String JSON_ASSERT_STATIC_IMPORT = 'com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson' private static final String JSON_ASSERT_CLASS = 'com.toomuchcoding.jsonassert.JsonAssertion' + private static final String REST_ASSURED_3_0_CLASS = 'io.restassured.RestAssured' + + @PackageScope ClassPresenceChecker checker = new ClassPresenceChecker() @Override String buildClass(ContractVerifierConfigProperties configProperties, Collection listOfFiles, String className, String classPackage, String includedDirectoryRelativePath) { @@ -62,7 +69,9 @@ class JavaTestGenerator implements SingleTestGenerator { addJsonPathRelatedImports(clazz) Map contracts = mapContractsToTheirTestTypes(listOfFiles) - + boolean restAssured3Present = this.checker.isClassPresent(REST_ASSURED_3_0_CLASS) + String restAssuredPackage = restAssured3Present ? 'io.restassured' : 'com.jayway.restassured' + log.info("Rest Assured version 3.0 found [${restAssured3Present}]") boolean conditionalImportsAdded = false boolean toIgnore = listOfFiles.ignored.find { it } contracts.each { ParsedDsl key, TestType value -> @@ -74,18 +83,18 @@ class JavaTestGenerator implements SingleTestGenerator { clazz.addImport('javax.ws.rs.core.Response') } } else if (configProperties.testMode == TestMode.MOCKMVC) { - clazz.addStaticImport('com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*') + clazz.addStaticImport("${restAssuredPackage}.module.mockmvc.RestAssuredMockMvc.*") } else { - clazz.addStaticImport('com.jayway.restassured.RestAssured.*') + clazz.addStaticImport("${restAssuredPackage}.RestAssured.*") } } if (configProperties.targetFramework == TestFramework.JUNIT) { if (contracts.values().contains(TestType.HTTP) && configProperties.testMode == TestMode.MOCKMVC) { - clazz.addImport('com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification') - clazz.addImport('com.jayway.restassured.response.ResponseOptions') + clazz.addImport("${restAssuredPackage}.module.mockmvc.specification.MockMvcRequestSpecification") + clazz.addImport("${restAssuredPackage}.response.ResponseOptions") } else if (contracts.values().contains(TestType.HTTP) && configProperties.testMode == TestMode.EXPLICIT) { - clazz.addImport('com.jayway.restassured.specification.RequestSpecification') - clazz.addImport('com.jayway.restassured.response.Response') + clazz.addImport("${restAssuredPackage}.specification.RequestSpecification") + clazz.addImport("${restAssuredPackage}.response.Response") } clazz.addImport('org.junit.Test') clazz.addStaticImport('org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat') @@ -151,7 +160,7 @@ class JavaTestGenerator implements SingleTestGenerator { clazz.addImport(['com.jayway.jsonpath.DocumentContext', 'com.jayway.jsonpath.JsonPath', ]) - if (jsonAssertPresent()) { + if (this.checker.isClassPresent(JSON_ASSERT_CLASS)) { clazz.addStaticImport(JSON_ASSERT_STATIC_IMPORT) } } @@ -168,16 +177,22 @@ class JavaTestGenerator implements SingleTestGenerator { clazz.addStaticImport('org.springframework.cloud.contract.verifier.messaging.util.ContractVerifierMessagingUtil.headers') } - private static boolean jsonAssertPresent() { +} + +class ClassPresenceChecker { + + private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass()) + + boolean isClassPresent(String className) { try { - Class.forName(JSON_ASSERT_CLASS) + + Class.forName(className) return true } catch (ClassNotFoundException e) { if (log.isDebugEnabled()) { - log.debug("JsonAssert is not present on classpath. Will not add a static import.") + log.debug("[${className}] is not present on classpath. Will not add a static import.") } return false } } - } diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/TestMode.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/TestMode.groovy index c23da52ae1..ab303b95f4 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/TestMode.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/TestMode.groovy @@ -25,12 +25,12 @@ package org.springframework.cloud.contract.verifier.config */ enum TestMode { /** - * Uses Spring's MockMvc + * Uses Spring's MockMvc with Rest Assured 2.x */ MOCKMVC, /** - * Uses direct HTTP invocations + * Uses direct HTTP invocations with Rest Assured 2.x */ EXPLICIT, diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy index 1bcecfe1c4..37b550602a 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy @@ -36,29 +36,51 @@ class SingleTestGeneratorSpec extends Specification { TemporaryFolder tmpFolder = new TemporaryFolder() File file - private static final List mockMvcJUnitClassStrings = ['package test; ', 'import com.jayway.jsonpath.DocumentContext; ', 'import com.jayway.jsonpath.JsonPath; ', - 'import org.junit.FixMethodOrder; ', 'import org.junit.Ignore; ', 'import org.junit.Test; ', 'import org.junit.runners.MethodSorters; ', - 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson; ', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*; ', - '@FixMethodOrder(MethodSorters.NAME_ASCENDING); ', '@Test; ', '@Ignore; ', 'import com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification; ', - 'import com.jayway.restassured.response.ResponseOptions; ', 'import static org.assertj.core.api.Assertions.assertThat'] + private static final List mockMvcJUnitClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', + 'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;', + 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*;', + '@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification;', + 'import com.jayway.restassured.response.ResponseOptions;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] + + private static final List mockMvcJUnitRestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', + 'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;', + 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;', + '@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import io.restassured.module.mockmvc.specification.MockMvcRequestSpecification;', + 'import io.restassured.response.ResponseOptions;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] - private static final List explicitJUnitClassStrings = ['package test; ', 'import com.jayway.jsonpath.DocumentContext; ', 'import com.jayway.jsonpath.JsonPath; ', - 'import org.junit.FixMethodOrder; ', 'import org.junit.Ignore; ', 'import org.junit.Test; ', 'import org.junit.runners.MethodSorters; ', - 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson; ', 'import static com.jayway.restassured.RestAssured.*; ', - '@FixMethodOrder(MethodSorters.NAME_ASCENDING); ', '@Test; ', '@Ignore; ', 'import com.jayway.restassured.specification.RequestSpecification; ', - 'import com.jayway.restassured.response.Response; ', 'import static org.assertj.core.api.Assertions.assertThat'] + private static final List explicitJUnitClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', + 'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;', + 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static com.jayway.restassured.RestAssured.*;', + '@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import com.jayway.restassured.specification.RequestSpecification;', + 'import com.jayway.restassured.response.Response;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] - private static final List spockClassStrings = ['package test', 'import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', + private static final List explicitJUnitRestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', + 'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;', + 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static io.restassured.RestAssured.*;', + '@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import io.restassured.specification.RequestSpecification;', + 'import io.restassured.response.Response;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] + + private static final List spockClassStrings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', 'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise', 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*', '@Stepwise', '@Ignore'] - private static final List explicitSpockClassStrings = ['package test', 'import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', + private static final List spockClassRestAssured3Strings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', + 'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise', + 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static io.restassured.module.mockmvc.RestAssuredMockMvc.*', + '@Stepwise', '@Ignore'] + + private static final List explicitSpockClassStrings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', 'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise', 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static com.jayway.restassured.RestAssured.*', '@Stepwise', '@Ignore'] + private static final List explicitSpockRestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', + 'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise', + 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static io.restassured.RestAssured.*', + '@Stepwise', '@Ignore'] + public static final Closure JAVA_ASSERTER = { String classToTest -> String name = Math.abs(new Random().nextInt()) String changedTest = classToTest.replace("public class Test", "public class Test${name}") @@ -94,6 +116,7 @@ class SingleTestGeneratorSpec extends Specification { given: ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() properties.targetFramework = testFramework + properties.testMode = mode ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convertAsCollection(file)) contract.ignored >> true contract.order >> 2 @@ -103,7 +126,7 @@ class SingleTestGeneratorSpec extends Specification { String clazz = testGenerator.buildClass(properties, [contract], "test", "test", 'com/foo') then: - classStrings.each { clazz.contains(it) } + classStrings.each { assert clazz.contains(it) } and: asserter(clazz) where: @@ -114,6 +137,36 @@ class SingleTestGeneratorSpec extends Specification { SPOCK | TestMode.EXPLICIT | explicitSpockClassStrings | GROOVY_ASSERTER } + def "should build test class for #testFramework with Rest Assured 3.0"() { + given: + ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() + properties.targetFramework = testFramework + properties.testMode = mode + ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2) + contract.ignored >> true + contract.order >> 2 + JavaTestGenerator testGenerator = new JavaTestGenerator(checker: new ClassPresenceChecker() { + @Override + boolean isClassPresent(String className) { + return true + } + }) + + when: + String clazz = testGenerator.buildClass(properties, [contract], "test", "test", 'com/foo') + + then: + classStrings.each { assert clazz.contains(it) } + !clazz.contains("com.jayway.restassured") + + where: + testFramework | mode | classStrings + JUNIT | TestMode.MOCKMVC | mockMvcJUnitRestAssured3ClassStrings + JUNIT | TestMode.EXPLICIT | explicitJUnitRestAssured3ClassStrings + SPOCK | TestMode.MOCKMVC | spockClassRestAssured3Strings + SPOCK | TestMode.EXPLICIT | explicitSpockRestAssured3ClassStrings + } + def "should build test class for #testFramework and mode #mode with two files"() { given: File file = tmpFolder.newFile() @@ -164,7 +217,7 @@ class SingleTestGeneratorSpec extends Specification { ContractMetadata contract2 = new ContractMetadata(file2.toPath(), false, 1, null) contract2.ignored >> false and: - JavaTestGenerator testGenerator = new JavaTestGenerator() + JavaTestGenerator testGenerator = new JavaTestGenerator() when: String clazz = testGenerator.buildClass(properties, [contract, contract2], "test", "test", 'com/foo')