diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiable.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiable.java index 63ac51ac40..3a7596ce7f 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiable.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiable.java @@ -68,7 +68,10 @@ class DelegatingJsonVerifiable implements MethodBufferingJsonVerifiable { return stringValue.replaceAll("\"", "\\\\\""); } - private static String wrapValueWithQuotes(Object value) { + static String wrapValueWithQuotes(Object value) { + if (value == null) { + return null; + } return value instanceof String ? "\"" + stringWithEscapedQuotes(value) + "\"" : value.toString(); } @@ -198,8 +201,8 @@ class DelegatingJsonVerifiable implements MethodBufferingJsonVerifiable { readyToCheck.methodsBuffer.offer(".value()"); } else { - readyToCheck.appendMethodWithValue("isEqualTo", - value instanceof Long ? String.valueOf(value).concat("L") : String.valueOf(value)); + readyToCheck.appendMethodWithValue("isEqualTo", value instanceof Long ? String.valueOf(value).concat("L") + : (value == null ? null : String.valueOf(value))); } return readyToCheck; } diff --git a/spring-cloud-contract-verifier/src/test/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiableTests.java b/spring-cloud-contract-verifier/src/test/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiableTests.java new file mode 100644 index 0000000000..3f6c9103ff --- /dev/null +++ b/spring-cloud-contract-verifier/src/test/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiableTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.verifier.util; + +import org.assertj.core.api.BDDAssertions; +import org.junit.jupiter.api.Test; + +class DelegatingJsonVerifiableTests { + + @Test + void shouldReturnNullValueForNull() { + String value = DelegatingJsonVerifiable.wrapValueWithQuotes(null); + + BDDAssertions.then(value).isNull(); + } + + @Test + void shouldReturnQuotedValueForString() { + String value = DelegatingJsonVerifiable.wrapValueWithQuotes("hello"); + + BDDAssertions.then(value).isEqualTo("\"hello\""); + } + + @Test + void shouldReturnStringValueForNonString() { + String value = DelegatingJsonVerifiable.wrapValueWithQuotes(5); + + BDDAssertions.then(value).isEqualTo("5"); + } + +}