2

I have perfectly running Mock API server based on Karate. There is some custom Java code that I also want to execute when Scenario is matched.

Executing the Mock using jar as follows

java -jar karate-0.9.0.jar -m ./src/test/java/Mymock.feature -p 9000

Mymock.feature has something like follows


  Scenario: pathMatches('/')
    * def Signature = Java.type('Signature')
    * def sign = Signature.calculate('382700b563f4', {'userId':'399645532', 'os':'android', 'client_key':'3c2cd3f3', 'token':'141a649988c946ae9b5356049c316c5d-838424771', 'token_client_salt':'d340a54c43d5642e21289f7ede858995'})
    * print sign.sig
    * print sign.__NStokensig

Code above works fine when executed directly via a Feature file.

But As expected this gives error if its invoked if embedded in Mocks.

Mymock.feature:70 - javascript evaluation failed: Java.type('Signature'), java.lang.ClassNotFoundException: Signature

How can I execute Java code embedded in mocks? I am not dependent on jar to execute Mocks so can easily try new way to execute Java Code in Mocks though any other method.

Amit Vyas
  • 103
  • 2
  • 8

1 Answers1

1

Since you are using Java I suggest you start the mock server via the Java API (embedding). This is what most teams do: https://github.com/intuit/karate/tree/master/karate-netty#embedding

And what happens then is the classpath will automatically include all the stuff you need, such as Signature.

But with a little effort you should be able to do what you want. What is happening is Signature is not on the classpath. Since you know Java, what you should do is a) make Signature available as a compiled *.class file or *.jar file somewhere, and then b) add it to the Java classpath.

EDIT: refer to this other post for the solution: https://stackoverflow.com/a/56458094/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Not able to do it with `java -cp some/folder -jar karate-0.9.0.jar` Will create a sample for this on github, Also will add example to start multiple mocks in there. using JAVA API I was able to at least start it for now. – Amit Vyas Jan 16 '19 at 14:54
  • I am still working on providing a sample for using Mock, please give me sometime on it. – Amit Vyas Jan 23 '19 at 14:52
  • 1
    @AmitVyas update - refer this for the solution: https://stackoverflow.com/a/56458094/143475 – Peter Thomas Jun 10 '19 at 14:55