Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

java - Executing Karate jar with mock using external library Spring Framework

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 '.argetest-classes' and contains the RabbitUtils.class file.

The current execution from workspace root looks like this:

java -jar C:intuitkarate-0.9.3.RC2.jar -cp .argetest-classes -p 6868 -m .srcestjavamocksapi1_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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...