kotoriのねこ
kotoriのねこ

Reputation: 11

Karate - when running tests with the karate.jar, `karate.callSingle('classpath:xxx.feature')` in karate-config.js fails with not found: xxx.feature

I put together a reproducible example of karate.callSingle() erroring out when it's run with the karate.jar at git repo. It is also the same repo that I post about down below. Want to see if I'm using the karate jar incorrectly.

My project has the following structure:

 |-src
 | |-test
 | | |-resources
 | | | |-karate-config.js
 | | | |-tests
 | | | | |-test1.feature
 | | | |-auth
 | | | | |-oauth2.feature
 | | |-java
 | | | |-AllTest.java
 |-pom.xml

karate-config.js has the following content:

function fn() {
    karate.configure('retry', { interval: 0 });

    let accessToken = '';

    if (karate.env === 'prod') {
        // Happy path
        accessToken = 'mock oauth2 access token';
    }
    if (karate.env !== 'prod') {
        // ERROR: this doesn't work when running as standalone jar
        const result = karate.callSingle('classpath:auth/oauth2.feature', {});
        accessToken = result.access_token;
    }

    return {
        access_token: accessToken,
    };
}

And under resources, the auth/oauth2.feature (pretty much just returns a mock access-token):

@ignore
Feature: oauth2

Scenario: Obtain oauth2 token
  * url 'https://example.com'
  * method get
  * status 200

  # Mock data here. In real use case, use the access token from response
  * def access_token = 'mock oauth2 access token'

And finally, a simple tests/test1.feature also under resources dir:

Feature: Access token

Scenario: Validate access token
  # consume access token obtained in config
  * url 'https://example.com'
  * method get
  * status 200

  * match access_token == 'mock oauth2 access token'

Now, when I go under src/test/resources and run java -jar /tmp/karate.jar ., I get the following error:

org.graalvm.polyglot.PolyglotException: not found: auth/oauth2.feature
- com.intuit.karate.resource.ResourceUtils.getResource(ResourceUtils.java:126)
- com.intuit.karate.core.ScenarioFileReader.toResource(ScenarioFileReader.java:129)
- com.intuit.karate.core.ScenarioFileReader.readFile(ScenarioFileReader.java:64)
- com.intuit.karate.core.ScenarioBridge.read(ScenarioBridge.java:731)
- com.intuit.karate.core.ScenarioBridge.callSingle(ScenarioBridge.java:229)
- <js>.fn(Unnamed:12)

I tried a bunch of different command line options, like setting the --workdir=$(pwd), --configdir=$(pwd), and also -cp /tmp/karate.jar:. (I think -cp won't work anyways when using java -jar), but to no avail. I got the same error message.

Is this the intended behavior that karate.callSingle('classpath:<something>) will not work at all when running via the standalone karate.jar?

My understanding from the karate docs was that, karate.callSingle('classpath:...') should work (and it does work) when it's invoked via maven. So, what do I need to do to get the standalone jar to work as well?

Any help is appreciated.

Upvotes: 1

Views: 440

Answers (0)

Related Questions