1

I am trying to read the karate config in my mock server as per the documentation at https://karatelabs.github.io/karate/karate-netty/#background

Feature: stateful mock server

  Background:
    * call read('classpath:karate-config.js')

  Scenario: pathMatches('/api/xml') && methodIs('post')
...
...

However i get an error on the line the call read is placed.

js failed:
>>>>
01: start('validateLocalCps.mock')
<<<<
org.graalvm.polyglot.PolyglotException: mock-server background failed - /cas_testing/auth/target/test-classes/auth/token/validateLocalCps.mock:4
- com.intuit.karate.core.MockHandler.<init>(MockHandler.java:116)
- com.intuit.karate.core.MockServer$Builder.build(MockServer.java:132)
- com.intuit.karate.core.ScenarioBridge.startInternal(ScenarioBridge.java:855)
- com.intuit.karate.core.ScenarioBridge.start(ScenarioBridge.java:817)
- <js>.:=>(Unnamed:1)


classpath:auth/token/validateLocalSuccess.feature:7

I am using karate version 1.2.0

My karate-config.js is inside src/test/java where as the feature files and mocks are inside other subdirectories.

How should i write this call so that i can read the config from my mock.

Gokul
  • 237
  • 3
  • 13

1 Answers1

0

Read up on what the classpath: means in the docs: https://github.com/karatelabs/karate#classpath

If still stuck, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

That said, teams usually never need to use karate-config.js in mocks, it should be used only for tests. My suggestion is just keep it simple, read some data from a file side-by-side with your mock if needed.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I need to read some system properties for my mock. I was using karate-config so that i could use karate.properties . would you recommend using java interop to directly read the property using System.getProperty instead of trying to read karate-config.js – Gokul Sep 02 '22 at 05:58
  • @Gokul if you are using Java to start your mock, there is a way in which you can inject properties into the mock via a `Map` so maybe you can do all the system property lookup in the java code before starting the mock: https://github.com/karatelabs/karate/blob/v1.2.1.RC1/karate-demo/src/test/java/mock/contract/ConsumerUsingMockTest.java#L28 - also see this: https://stackoverflow.com/a/52821230/143475 - you may be able to do `java.lang.System.getProperty()` – Peter Thomas Sep 02 '22 at 06:11
  • 1
    Thanks @Peter Thomas. Used java.lang.System.getProperty() and it worked without a hitch. – Gokul Sep 02 '22 at 06:31
  • @Gokul thanks ! so looks like `karate.properties[]` doesn't apply to mocks, this discussion will help others – Peter Thomas Sep 02 '22 at 06:37