diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java index 975a6c7658d..3d3ecb3d1e1 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; @@ -139,15 +140,41 @@ private void doAssert(String jsonString) { if (isExpectNull()) { throw new IllegalStateException(String.format("Value in json path '%s' expected to be null, but found '%s'", getJsonPath(), value)); + } else if (isUseRegex()) { + throw new IllegalStateException(String.format( + "Value in json path '%s' expected to match regexp '%s', but it did not match: '%s'", + getJsonPath(), getExpectedValue(), objectToString(value))); } else { - String msg; - if (isUseRegex()) { - msg = "Value in json path '%s' expected to match regexp '%s', but it did not match: '%s'"; - } else { - msg = "Value in json path '%s' expected to be '%s', but found '%s'"; + Object expected = JSONValue.parse(getExpectedValue()); + String expectedType = jsonTypeOf(expected); + String actualType = jsonTypeOf(value); + // Values like "8" and 8 print the same, so name the types when they differ + if (expected != null && !expectedType.equals(actualType)) { + throw new IllegalStateException(String.format( + "Value in json path '%s' expected to be %s '%s', but found %s '%s'", + getJsonPath(), expectedType, getExpectedValue(), actualType, objectToString(value))); } - throw new IllegalStateException(String.format(msg, getJsonPath(), getExpectedValue(), objectToString(value))); + throw new IllegalStateException(String.format( + "Value in json path '%s' expected to be '%s', but found '%s'", + getJsonPath(), getExpectedValue(), objectToString(value))); + } + } + + private static String jsonTypeOf(Object value) { + if (value == null) { + return "null"; + } else if (value instanceof String) { + return "string"; + } else if (value instanceof Number) { + return "number"; + } else if (value instanceof Boolean) { + return "boolean"; + } else if (value instanceof List) { + return "array"; + } else if (value instanceof Map) { + return "object"; } + return value.getClass().getSimpleName(); } private boolean arrayMatched(JSONArray value) { diff --git a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java b/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java index f39fe6d08d5..35268a51602 100644 --- a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java +++ b/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java @@ -21,14 +21,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; import java.nio.charset.Charset; import java.util.Locale; +import java.util.stream.Stream; import org.apache.jmeter.samplers.SampleResult; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; class TestJSONPathAssertion { @@ -348,6 +352,39 @@ void testGetResult_match_msg_problem2() { assertFalse(result.isFailure()); } + static Stream failureMessages() { + return Stream.of( + arguments("{\"age\": \"8\"}", "8", + "Value in json path '$.age' expected to be number '8', but found string '8'"), + arguments("{\"age\": 8}", "\"8\"", + "Value in json path '$.age' expected to be string '\"8\"', but found number '8'"), + arguments("{\"age\": \"true\"}", "true", + "Value in json path '$.age' expected to be boolean 'true', but found string 'true'"), + arguments("{\"age\": [7]}", "8", + "Value in json path '$.age' expected to be number '8', but found array '[7]'"), + arguments("{\"age\": 8}", "9", + "Value in json path '$.age' expected to be '9', but found '8'"), + arguments("{\"age\": \"8\"}", "\"9\"", + "Value in json path '$.age' expected to be '\"9\"', but found '8'") + ); + } + + @ParameterizedTest + @MethodSource("failureMessages") + void testGetResult_failureMessageNamesTypes(String data, String expectedValue, String expectedMessage) { + SampleResult samplerResult = new SampleResult(); + samplerResult.setResponseData(data.getBytes(Charset.defaultCharset())); + + JSONPathAssertion instance = new JSONPathAssertion(); + instance.setJsonPath("$.age"); + instance.setJsonValidationBool(true); + instance.setIsRegex(false); + instance.setExpectedValue(expectedValue); + AssertionResult result = instance.getResult(samplerResult); + assertTrue(result.isFailure()); + assertEquals(expectedMessage, result.getFailureMessage()); + } + @Test void testGetResultFloat() { Locale prevLocale = Locale.getDefault(); diff --git a/xdocs/changes.xml b/xdocs/changes.xml index dd7f129c73f..9f299949223 100644 --- a/xdocs/changes.xml +++ b/xdocs/changes.xml @@ -179,6 +179,7 @@ Summary
  • 6873Show the test type, such as text expected to contain, in Response Assertion failure messages when the Or option is on.
  • 6873Use the default value in the Boundary Extractor with variable scope when a boundary is empty and Match No. is above 1, as sample scope already did. An empty boundary gives at most one match.
  • 67596240Regular Expression Extractor created a NumberFormatException (caught and discarded) on every failed extraction. Failed extractions no longer create exceptions when cleaning up the group variables.
  • +
  • 5745Name the JSON types in JSON Assertion failure messages when the expected and the actual value have different types, so "8" and 8 no longer fail with expected to be '8', but found '8'.
  • HTTP Samplers and Test Script Recorder