wrapValueWithQuotes accepts nulls
without this change NPE is thrown fixes gh-2117
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user