1

I am storing my expected json string in the json file under resources as shown below. The json string consists of regular expression. I am using JSONAssert Library to compare two json strings.

{
  "timestamp": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}+\\d{4}$",
  "status": 415,
  "error": "Unsupported Media Type",
  "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",
  "path": "/service/addUser"
}

My actual response consists of timestamp in this format 2018-11-13T04:10:11.233+0000

    JSONAssert.assertEquals(getJsonBody(expected), response.asString(),false);

Is always giving the below error on the regular expression

java.lang.AssertionError: timestamp
 Expected: ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}+\d{4}$
      got: 2018-11-13T04:12:55.923+0000

Any recommendation on this error?

7
  • instead of comparing values, you should do something like assertTrue(response.asString().matches(expected)) Commented Nov 13, 2018 at 4:19
  • 1
    Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}\+\d{4}$ Commented Nov 13, 2018 at 4:23
  • I did escaped but it doesn't work. Same error Commented Nov 13, 2018 at 4:42
  • I have added this regex [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}\\+[0-9]{4} but didnt worked Commented Nov 13, 2018 at 4:53
  • @Kartik I got some pattern matcher exception. Let me fix that Commented Nov 13, 2018 at 4:57

1 Answer 1

4

You are comparing the pattern with the timestamp string. What you actually need to do is check if the timestamp matches the pattern.

Try this code:-

String expected = "{\n" +
        "  \"timestamp\": \"^\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}\\\\.\\\\d{3}\\\\+\\\\d{4}$\",\n" +
        "  \"status\": 415,\n" +
        "  \"error\": \"Unsupported Media Type\",\n" +
        "  \"message\": \"Content type 'text/plain;charset=ISO-8859-1' not supported\",\n" +
        "  \"path\": \"/service/addUser\"\n" +
        "}";
String actual = "{\n" +
        "  \"timestamp\": \"2018-11-13T04:12:55.923+0000\",\n" +
        "  \"status\": 415,\n" +
        "  \"error\": \"Unsupported Media Type\",\n" +
        "  \"message\": \"Content type 'text/plain;charset=ISO-8859-1' not supported\",\n" +
        "  \"path\": \"/service/addUser\"\n" +
        "}";
JSONAssert.assertEquals(
        expected,
        actual,
        new CustomComparator(
                JSONCompareMode.LENIENT,
                new Customization("***", new RegularExpressionValueMatcher<>())
        )
);

So with your code, it will look something like:-

JSONAssert.assertEquals(
        getJsonBody(expected),
        response.asString(),
        new CustomComparator(
                JSONCompareMode.LENIENT,
                new Customization("***", new RegularExpressionValueMatcher<>())
        )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.