Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -348,6 +352,39 @@ void testGetResult_match_msg_problem2() {
assertFalse(result.isFailure());
}

static Stream<Arguments> 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();
Expand Down
1 change: 1 addition & 0 deletions xdocs/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Summary
<li><pr>6873</pr>Show the test type, such as <code>text expected to contain</code>, in Response Assertion failure messages when the <code>Or</code> option is on.</li>
<li><pr>6873</pr>Use 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.</li>
<li><pr>6759</pr><issue>6240</issue>Regular Expression Extractor created a <code>NumberFormatException</code> (caught and discarded) on every failed extraction. Failed extractions no longer create exceptions when cleaning up the group variables.</li>
<li><issue>5745</issue>Name the JSON types in JSON Assertion failure messages when the expected and the actual value have different types, so <code>"8"</code> and <code>8</code> no longer fail with <code>expected to be '8', but found '8'</code>.</li>
</ul>

<h3>HTTP Samplers and Test Script Recorder</h3>
Expand Down