4

My validation is to verify weather orgId appears in the response or not and that orgId should contain some value

The response that i am getting is Status code 200 and response body is empty.

Now I have below implementation

Then match $.orgId == '#present'
Then match $.orgId == '#notnull'

In this case the code passes, Ideally it should fail since response body is empty and orgId is not present in response. My question is why is the code getting passed with #present and #notnull even if the response body is empty

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
Archit Goel
  • 63
  • 1
  • 2
  • 5

1 Answers1

8

You are definitely missing something. Try this in a fresh scenario and see it work. We are hard-coding response below, which is exactly equivalent to what happens at run-time, and by the way this is a great way for you to test assertions against different types of JSON (without making any HTTP calls):

* def response = {}
Then match $.orgId == '#present'
Then match $.orgId == '#notnull'

And this gives a failure as you expect:

assertion failed: path: $.orgId, actual: null, expected: '#present', reason: actual json-path does not exist

So if you still are stuck, follow this process please: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

EDIT: if your response is an empty string but you were expecting JSON, just do this and it will fail the test, refer type-conversion: https://github.com/intuit/karate#type-conversion

* json response = response

But as mentioned in the docs, you should always try to match "full JSON" so this should work:

* def response = ''
Then match $ contains { orgId: '#notnull' }

EDIT: this will be fixed in 0.9.4 https://github.com/intuit/karate/issues/814

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • The scenario that i am talking about is little different . The response is not {} The response is actually empty * def response = '' Then match $.orgId == '#present' Then match $.orgId == '#notnull' This will not give a failure, Ideally it should fail – Archit Goel Jun 27 '19 at 08:50
  • @ArchitGoel your question was not clear at all. anyway, see my edit. – Peter Thomas Jun 27 '19 at 08:56
  • I saw your edit and added as below * def response = '' * json response = response Then match $.orgId == '#present' Then match $.orgId == '#notnull' This gets failed by saying that cannot convert not a json string – Archit Goel Jun 27 '19 at 09:12
  • This is just a work around, my question is why it should fail by saying cannot convert to a json string. It should ideally fail by saying orgId is not present or something like that. #notnull or #present is not even executed in this case – Archit Goel Jun 27 '19 at 09:23
  • 1
    @ArchitGoel https://github.com/intuit/karate/issues/814 - any questions ? – Peter Thomas Jun 27 '19 at 09:32
  • Thanks for raising this as a bug, will try to implement it. – Archit Goel Jun 27 '19 at 10:04
  • @PeterThomas I know this question is old and was probably fixed on version 0.9.4, but I'm using version 0.9.5 and it's still happening when the body is [] – vianna77 Oct 29 '21 at 13:41
  • @vianna77 and I'm sorry we are not supporting old versions – Peter Thomas Oct 29 '21 at 13:43