2

I have a use case where I want to mock an API /register that returns some date but then also calls another REST API from another service after it returns data to this request.

The feature file looks something like this:

  Scenario: pathMatches('/register')
    * def responseStatus = 200
    * def response = { registerId: '1234' }
    # do the callback request after the response is returned
    * karate.http('http://someservice.com/callback').get()

But this does not work in Karate as it runs synchronously. I also tried to create a new thread but this is not supported by GraalJS and throws an Exception.

Is there some other way to do this?

1 Answers1

1

I think it would work if you call a Java helper - and then do some heavly lifting in that helper. Refer this example - that does an async message send operation from the mock: https://twitter.com/getkarate/status/1417023536082812935

Yes, Graal JS has some very severe limitations :( - but if you keep the flow in "pure java", or rather, do the "new thread" stuff from the Java side, it should work.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    This would probably work. But we are using karate in the standalone JAR version which would make it more complicated to also include some other JAR which has the pure functionality to do the async messaging. Is there no other way to do this besides going to pure Java? Still thanks for your help! – LingusTerminus Mar 24 '23 at 08:10
  • 1
    @LingusTerminus agree. you can find tips on the JAR + classpath here: https://stackoverflow.com/a/56458094/143475 - 2 other options a) build your own JAR, b) create a custom Docker container – Peter Thomas Mar 24 '23 at 08:28