Browse Source

Merge branch '5.2.x'

pull/25507/head
Sam Brannen 6 years ago
parent
commit
d8554c498c
  1. 15
      spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java
  2. 26
      spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java
  3. 14
      spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java

15
spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

@ -26,6 +26,7 @@ import org.hamcrest.MatcherAssert;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -108,10 +109,18 @@ public class JsonPathExpectationsHelper {
} }
actualValue = actualValueList.get(0); actualValue = actualValueList.get(0);
} }
else if (actualValue != null && expectedValue != null) { else if (actualValue != null && expectedValue != null &&
if (!actualValue.getClass().equals(expectedValue.getClass())) { !actualValue.getClass().equals(expectedValue.getClass())) {
try {
actualValue = evaluateJsonPath(content, expectedValue.getClass()); actualValue = evaluateJsonPath(content, expectedValue.getClass());
} }
catch (AssertionError error) {
String message = String.format(
"At JSON path \"%s\", value <%s> of type <%s> cannot be converted to type <%s>",
this.expression, actualValue, ClassUtils.getDescriptiveType(actualValue),
ClassUtils.getDescriptiveType(expectedValue));
throw new AssertionError(message, error.getCause());
}
} }
AssertionErrors.assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue); AssertionErrors.assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
} }
@ -298,7 +307,7 @@ public class JsonPathExpectationsHelper {
/** /**
* Variant of {@link #evaluateJsonPath(String)} with a target type. * Variant of {@link #evaluateJsonPath(String)} with a target type.
* This can be useful for matching numbers reliably for example coercing an * <p>This can be useful for matching numbers reliably for example coercing an
* integer into a double. * integer into a double.
* @param content the content to evaluate against * @param content the content to evaluate against
* @return the result of the evaluation * @return the result of the evaluation

26
spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2004-2019 the original author or authors. * Copyright 2004-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -67,12 +67,12 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void existsForIndefinatePathWithResults() throws Exception { void existsForIndefinitePathWithResults() throws Exception {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").exists(SIMPSONS); new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").exists(SIMPSONS);
} }
@Test @Test
void existsForIndefinatePathWithEmptyResults() throws Exception { void existsForIndefinitePathWithEmptyResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]"; String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).exists(SIMPSONS)) new JsonPathExpectationsHelper(expression).exists(SIMPSONS))
@ -101,7 +101,7 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void doesNotExistForIndefinatePathWithResults() throws Exception { void doesNotExistForIndefinitePathWithResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Bart')]"; String expression = "$.familyMembers[?(@.name == 'Bart')]";
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS)) new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS))
@ -109,7 +109,7 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void doesNotExistForIndefinatePathWithEmptyResults() throws Exception { void doesNotExistForIndefinitePathWithEmptyResults() throws Exception {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotExist(SIMPSONS); new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotExist(SIMPSONS);
} }
@ -129,12 +129,12 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void assertValueIsEmptyForIndefinatePathWithEmptyResults() throws Exception { void assertValueIsEmptyForIndefinitePathWithEmptyResults() throws Exception {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").assertValueIsEmpty(SIMPSONS); new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").assertValueIsEmpty(SIMPSONS);
} }
@Test @Test
void assertValueIsEmptyForIndefinatePathWithResults() throws Exception { void assertValueIsEmptyForIndefinitePathWithResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Bart')]"; String expression = "$.familyMembers[?(@.name == 'Bart')]";
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS)) new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS))
@ -175,12 +175,12 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void assertValueIsNotEmptyForIndefinatePathWithResults() throws Exception { void assertValueIsNotEmptyForIndefinitePathWithResults() throws Exception {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").assertValueIsNotEmpty(SIMPSONS); new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").assertValueIsNotEmpty(SIMPSONS);
} }
@Test @Test
void assertValueIsNotEmptyForIndefinatePathWithEmptyResults() throws Exception { void assertValueIsNotEmptyForIndefinitePathWithEmptyResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]"; String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS)) new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS))
@ -222,12 +222,12 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void hasJsonPathForIndefinatePathWithResults() { void hasJsonPathForIndefinitePathWithResults() {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").hasJsonPath(SIMPSONS); new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").hasJsonPath(SIMPSONS);
} }
@Test @Test
void hasJsonPathForIndefinatePathWithEmptyResults() { void hasJsonPathForIndefinitePathWithEmptyResults() {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]"; String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS)) new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS))
@ -246,12 +246,12 @@ class JsonPathExpectationsHelperTests {
} }
@Test @Test
void doesNotHaveJsonPathForIndefinatePathWithEmptyResults() { void doesNotHaveJsonPathForIndefinitePathWithEmptyResults() {
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotHaveJsonPath(SIMPSONS); new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotHaveJsonPath(SIMPSONS);
} }
@Test @Test
void doesNotHaveEmptyPathForIndefinatePathWithResults() { void doesNotHaveEmptyPathForIndefinitePathWithResults() {
String expression = "$.familyMembers[?(@.name == 'Bart')]"; String expression = "$.familyMembers[?(@.name == 'Bart')]";
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS)) new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS))

14
spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java

@ -64,9 +64,17 @@ public class JsonPathResultMatchersTests {
} }
@Test @Test
public void valueWithMismatch() throws Exception { public void valueWithValueMismatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class)
new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult)); .isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult))
.withMessage("JSON path \"$.str\" expected:<bogus> but was:<foo>");
}
@Test
public void valueWithTypeMismatch() throws Exception {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus".getBytes()).match(stubMvcResult))
.withMessage("At JSON path \"$.str\", value <foo> of type <java.lang.String> cannot be converted to type <byte[]>");
} }
@Test @Test

Loading…
Cancel
Save