diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JsonBodyVerificationBuilder.java b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JsonBodyVerificationBuilder.java index 98531e1d56..b36653b30a 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JsonBodyVerificationBuilder.java +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JsonBodyVerificationBuilder.java @@ -16,6 +16,7 @@ package org.springframework.cloud.contract.verifier.builder; +import java.math.BigDecimal; import java.util.Map; import java.util.Optional; import java.util.function.Function; @@ -178,7 +179,7 @@ class JsonBodyVerificationBuilder implements BodyMethodGeneration, ClassVerifier else { String comparisonMethod = bodyMatcher.matchingType() == MatchingType.EQUALITY ? "isEqualTo" : "matches"; - String classToCastTo = retrievedValue.getClass().getSimpleName() + ".class"; + String classToCastTo = className(retrievedValue) + ".class"; String method = "assertThat(parsedJson.read(" + path + ", " + classToCastTo + "))." + comparisonMethod + "(" + valueAsParam + ")"; bb.addLine(postProcessJsonPathCall.apply(method)); @@ -186,9 +187,20 @@ class JsonBodyVerificationBuilder implements BodyMethodGeneration, ClassVerifier addColonIfRequired(lineSuffix, bb); } + private String className(Object retrievedValue) { + return retrievedValue.getClass().getName().startsWith("java.lang") ? + retrievedValue.getClass().getSimpleName() : retrievedValue.getClass().getName(); + } + private String objectToString(Object value) { - return value instanceof Long ? String.valueOf(value).concat("L") - : String.valueOf(value); + if (value instanceof Long) { + return String.valueOf(value).concat("L"); + } else if (value instanceof Double) { + return String.valueOf(value).concat("D"); + } else if (value instanceof BigDecimal) { + return quotedAndEscaped(value.toString()); + } + return String.valueOf(value); } protected String processIfTemplateIsPresent(String method, diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SpringTestMethodBodyBuildersSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SpringTestMethodBodyBuildersSpec.groovy index 9794a874e4..bc7057ef7e 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SpringTestMethodBodyBuildersSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SpringTestMethodBodyBuildersSpec.groovy @@ -32,7 +32,6 @@ import org.springframework.cloud.contract.verifier.config.TestMode import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubVerifier import org.springframework.cloud.contract.verifier.file.ContractMetadata import org.springframework.cloud.contract.verifier.util.SyntaxChecker - /** * @author Jakub Kubrynski, codearte.io * @author Tim Ysewyn @@ -2963,4 +2962,54 @@ DocumentContext parsedJson = JsonPath.parse(json); } "webclient" | { properties.testMode = TestMode.WEBTESTCLIENT } } + + @Issue('#1139') + def 'should have a big decimal import for [#methodBuilderName]'() { + given: + Contract contractDsl = Contract.make { + request { + method 'POST' + url '/crystals/create' + headers { + header 'Content-Type' : 'application/json' + } + body( + amount: 200 + ) + bodyMatchers { + jsonPath('$.amount', byRegex('^[0-9]{1,3}$')) + } + } + response { + status 201 + body( + amount: fromRequest().body('$.amount'), + price: 10100.0 + ) + bodyMatchers { + jsonPath('$.amount', byRegex('^\\d*$')) + jsonPath('$.price', byEquality()) + } + } + } + methodBuilder() + when: + String test = singleTestGenerator(contractDsl) + then: + SyntaxChecker.tryToCompile(methodBuilderName, test) + then: + test.contains('''$.price", java.math.BigDecimal.class)).isEqualTo("10100.0")''') + where: + methodBuilderName | methodBuilder + "spock" | { properties.testFramework = TestFramework.SPOCK } + "testng" | { properties.testFramework = TestFramework.TESTNG } + "mockmvc" | { properties.testMode = TestMode.MOCKMVC } + "jaxrs-spock" | { + properties.testFramework = TestFramework.SPOCK; properties.testMode = TestMode.JAXRSCLIENT + } + "jaxrs" | { + properties.testFramework = TestFramework.JUNIT; properties.testMode = TestMode.JAXRSCLIENT + } + "webclient" | { properties.testMode = TestMode.WEBTESTCLIENT } + } }