karatelabs / karate

Test Automation Made Simple
https://karatelabs.github.io/karate
MIT License
8.29k stars 1.95k forks source link

Error trying to execute karate tests from the Main folder - Java #2241

Closed juanfelipemontoyac closed 1 year ago

juanfelipemontoyac commented 1 year ago

All the best! I am currently developing an api in Java with Javalin, Reactor-core and Guice. What my api does is expose an endpoint to run some Karate tests. Because of this, we had to move the Karate folder that was normally found in Test to the Main Folder in order to call it from a main class: Project structure: -src --main ---java ----com.myproject -----controllers ------Controller.java -----Main.java ----features -----Scenario.feature -----karate-config.js The way I'm trying to call the karate tests from the Controller.java class is as follows:

public String runTestsParallel() {
    final Results results = Runner.path("./src/main/features")
        .outputCucumberJson(Boolean.TRUE)
        .tags("~@ignore")
        .parallel(10);
    generateReport(results.getReportDir());
    final String errorMessage = String.format(
        "Errors count: %s. Errors messages: %s",
        results.getFailCount(),
        results.getErrorMessages()
    );
    if (results.getFailCount() > 0) {
        throw new IllegalStateException(errorMessage);
    }
    return String.format("ALL TESTS COMPLETED SUCCESSFULLY. %s", errorMessage);
}

private static void generateReport(final String karateOutputPath) {
    final Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] {"json"}, true);
    final List<String> jsonPaths = new ArrayList<>(jsonFiles.size());
    jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
    final Configuration config = new Configuration(new File("target"), "my-project");
    final ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
    reportBuilder.generateReports();
}

This implementation works when I manually run the Main.java class, but when running the app with gradle run I get this error:

Caused by: java.lang.IllegalArgumentException: Could not find option with name js.ecmascript-version. at com.oracle.truffle.polyglot.PolyglotEngineException.illegalArgument(PolyglotEngineException.java:131) at com.oracle.truffle.polyglot.OptionValuesImpl.failNotFound(OptionValuesImpl.java:274) at com.oracle.truffle.polyglot.OptionValuesImpl.failNotFound(OptionValuesImpl.java:274) at com.oracle.truffle.polyglot.PolyglotContextConfig.findObjectForContextOption(PolyglotContextConfig.java:479) at com.oracle.truffle.polyglot.PolyglotContextConfig.(PolyglotContextConfig.java:284) at com.oracle.truffle.polyglot.PolyglotEngineImpl.createContext(PolyglotEngineImpl.java:1732) at com.oracle.truffle.polyglot.PolyglotEngineDispatch.createContext(PolyglotEngineDispatch.java:162) at org.graalvm.polyglot.Context$Builder.build(Context.java:1861) at com.intuit.karate.graal.JsEngine.createContext(JsEngine.java:79) at com.intuit.karate.graal.JsEngine$1.initialValue(JsEngine.java:63) at com.intuit.karate.graal.JsEngine$1.initialValue(JsEngine.java:60) at java.base/java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:195) at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:172) at com.intuit.karate.graal.JsEngine.local(JsEngine.java:99) at com.intuit.karate.report.Report$Builder.build(Report.java:122) at com.intuit.karate.report.SuiteReports.timelineReport(SuiteReports.java:68) at com.intuit.karate.Results.(Results.java:98) at com.intuit.karate.Results.of(Results.java:57) at com.intuit.karate.Suite.buildResults(Suite.java:384) at com.intuit.karate.Runner$Builder.parallel(Runner.java:497)

The versions of karate and cucumber that I am using are:

implementation 'com.intuit.karate:karate-junit5:1.3.1' implementation 'net.masterthought:cucumber-reporting:5.7.4'

I tried to set the environment variable of js.ecmascript-version=11 but it didn't work

Is there any specific configuration that needs to be taken into account for this to work with the gradle run command?

ptrthomas commented 1 year ago

@juanfelipemontoyac never seen this before sounds very specific to your environment. can reopen if you follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

juanfelipemontoyac commented 1 year ago

Hi @ptrthomas ! Yes, it's a very specific need, but I managed to find the error, that's why I closed the issue. The problem was in the configuration that I had done for the gradle run task, I just put a default configuration and it works fine!

ptrthomas commented 1 year ago

@juanfelipemontoyac oh okay, glad it works !