3

We have a custom zio.test.Assertion that wrap json-path-assert:

  def isJson(matching: org.hamcrest.Matcher[_ >: ReadContext]): Assertion[HttpEntity] = {
    def test(entity: => HttpEntity): Boolean = {
      // Removed for brevity
    }
    Assertion.assertion[HttpEntity]("isJson")(param(matching))(test)
  }

We are quite happy with it but the failure message is far to be obvious:

response.body = Strict(ByteString(121, 32, 95, 111, 10, 108, 113, 35, 50, 90, 124, 23, 112, 111, 112, 31, 48, 24, 46, 31, 111, 22, 111, 25, 93, 52, 41, 54, 59, 90, 100, 43, 42, 51, 75, 14, 41, 54, 40, 65, 34, 42, 99, 88, 100, 54, 34, 25, 33, 89, 41, 40, 54, 52, 44, 40, 31, 101, 112, 105, 91, 10, 35, 125, 79, 106, 95, 11, 11, 32, 85, 13, 43, 104, 111)... and [661] more,Some(application/json)) did not satisfy isJson(with json path "$['store']['name']")

I would like to print the Json string decoded from those bytes but I do not see how to do that.

How can we customize the messages of a custom zio.test.Assertion?

Thanks

gervais.b
  • 2,294
  • 2
  • 22
  • 46
  • would you consider mapping the error in the business logic? – gurghet Oct 13 '21 at 13:55
  • Do you mean, using code in addition to the assertion itself? If that's the case, no. We would like to have one assertion that works out-of-the-box, without anything else. – gervais.b Oct 14 '21 at 07:48

1 Answers1

0

The param(matching) means that you are using the toString method of matching to render the the input of the assertion.

You can pass the decoded json string instead to have that display. For example (assuming your json is UTF-8 encoded):

 Assertion.assertion[HttpEntity.Strict]("isJson")
     (param(matching.data.decodeString(StandardCharsets.UTF_8))(test)

That particular example only works on HttpEntity.Strict but can be generalized to HttpEntity if you access the entity contents with .dataStream

AlexV
  • 449
  • 3
  • 6