3

Good morning. I am working on a project that uses Karate Standalone. I am completely new to Karate to excuse my lack of knowledge here.

The standalone karate jar is executed with the '-m' command line parameter to start a mock.feature. The mock.feature references a utils class that is built on 'org.springframework.amqp'.

The problem is that the karate.jar startup fails with a Command Line Execution Exception due to external library 'org/springframework/amqp/rabbit/connection/ConnectionFactory'

api1_mock_test.feature

Feature: API1 Mock Test

Background:
* def RabbitUtils = Java.type('utils.RabbitUtils')
.
.

Our RabbitUtils is just a java class that imports org.springframework.amqp external libraries to provide functions to interact with a Rabbit AMQP broker e.g. connect, receive, publish, purge etc. When built and run in IntelliJ all works ok. The POM reference in the project is:

<dependency>
   <groupId>org.springframework.amqp</groupId>
   <artifactId>spring-rabbit</artifactId>
   <version>2.1.5.RELEASE</version>
</dependency>

Does the Karate standalone jar have some way of referencing external libraries? The classpath parameter is set to reference our workspace '.\target\test-classes' and contains the RabbitUtils.class file.

The current execution from workspace root looks like this:

java -jar C:\intuit\karate-0.9.3.RC2.jar -cp .\target\test-classes -p 6868 -m .\src\test\java\mocks\api1_mock_test.feature
08:57:05.122 [main] INFO com.intuit.karate.Main - Karate version: 0.9.3.RC2
08:57:05.891 [main] ERROR com.intuit.karate - server-side background init failed - api1_mock_test.feature:4
Exception in thread "main" picocli.CommandLine$ExecutionException:
-unknown-:4 - org/springframework/amqp/rabbit/connection/ConnectionFactory

Thank you!

mactwixs
  • 402
  • 1
  • 6
  • 15

1 Answers1

4

Thanks for asking this, and I think I've figured out a way to do this which opens up a lot of great possibilities. The solution is to use Java first-principles, and not use the -jar option. The Karate command-line-app (or CLI) class happens to be com.intuit.karate.Main. I'm going to provide a demo here of using SikuliX. First, the feature file test.feature:

Feature: sikuli test

Background:
* def Screen = Java.type('org.sikuli.script.Screen')

Scenario:
* def s = new Screen()
* def c = s.capture()
* c.getFile('.')

And with the karate.jar and sikulixapi.jar in the same folder on the command line, this works (for windows, use ; instead of : as the "path separator"):

java -cp karate.jar:sikulixapi.jar com.intuit.karate.Main test.feature

For those looking to customize the classpath for the Visual Studio Code "Karate Runner" extension, please refer this: https://github.com/intuit/karate/wiki/Karate-Robot-Windows-Install-Guide#change-command-line-settings

Also see: https://stackoverflow.com/a/58398958/143475

For those who really don't want to compile Java but need to use some JVM libraries, it is possible via pure JS, (but hard to troubleshoot and debug): https://stackoverflow.com/a/65035825/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hi Peter. Many thanks for quick response & thanks for the excellent tool. Much appreciated. Can see how that works to stand up feature server mocks (is use of '-m' & '-p' the same?). Our use case is testing standalone Win services that talk to API & MQ, therefore we use standalone server to host mocks that mock out API & MQ comms. Utils classes written in Java that are referenced in the test.features & mock.features. All ext dependencies are held in local .m2 cache so would need a way of copying them over, think IntelliJ can handle that. Would this be a demo under git hub eventually? Cheers. – mactwixs Jun 05 '19 at 12:02
  • @mactwixs thanks :) `-p` is mandatory for the port. I think there are ways to copy JARs out of maven, e.g: https://stackoverflow.com/a/996915/143475 - also someone told me this trick: `mvn dependency:build-classpath -Dmdep.includeScope=test -Dmdep.outputFile=classpath.txt` to dump the classpath to a file. this is very specific so not sure if this can be a demo, but I would encourage you to blog about your experience etc – Peter Thomas Jun 05 '19 at 12:51
  • @PeterThomas As my question was made duplicate, i tried using "java -cp karate.jar. com.intuit.karate.Main -e qa Login.feature" But still i see the error >>> failed features: js failed: >>>> 01: Java.type('test') <<<< org.graalvm.polyglot.PolyglotException: TypeError: Access to host class test is not allowed or does not exist. - .:program(Unnamed:1) Login.feature:10 << – Sandeep P Jul 10 '21 at 13:57
  • @SandeepP follow this process - or if you can't - please assume that what you want is not supported by karate: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Jul 10 '21 at 14:22