kpschmidt
kpschmidt

Reputation: 245

Cannot create Dynamic Scenario Outline via Java call

Cannot create Dynamic Scenario Outline via Java call in Karate.

I can create a Dynamic Scenario Outline with "hard-coded" Json Array for example:

* def foobar = [{ 'type': 'app' }]

But when I attempt to generate the same Json Array from a Java class, I always get the following Karate warning(s) and the Dynamic Scenario Outline never executes:

WARN com.intuit.karate - ignoring dynamic expression, did not evaluate to list

-- OR --

WARN com.intuit.karate - ignoring dynamic expression list item 0, not map-like

I've tried using the Karate key-words 'def', 'string', 'json' as the var type but no luck. I've even tried hard-coding the same string shown above in the Java method with no luck.

I declare/call my Java class in 'Background:' and print what is given back and it "looks" correct.

Background:
 * def IdaDataApiUtil = Java.type('data.IdaDataApiUtil')
 * def foobar = IdaDataApiUtil.getClientExample('ida-sp')
 * print foobar

I then try to utilize the JsonArray in my 'Example:' like such:

Examples:
 | foobar |

At this point I get the above mentioned error(s), depending on what I've tried to return (JsonArray, JsonObject, Map, List).

If I simply use the hard-coded 'def':

* def foobar = [{ 'type': 'app' }]

It works as expected.

In my Java code I've tried various things:

Hard-coded Json string:

public static String getClientExample() {
    return "[{ 'type': 'app' }]";
}

List:

public static List<String> getClientExample() {
    List<String> list = new ArrayList<>();
    list.add("'type': 'app'");
    return list
}

Map:

public static Map<String, Object> getClientExample() {
    Map<String, Object> map = new HashMap<>();
    map.put("type", "app");
    return map;
}

I've played with variations of key/values in both list/map with no luck. I've also tried with JSONObject/JSONArray but no luck there as well.

I feel like I'm missing something vary obvious but I can't see the forest through the trees at the moment...

Upvotes: 2

Views: 746

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58153

I attempt to generate the same Json Array from a Java class,

What you return from the Java code should be a List<Map<String, Object>> and it should work fine. You have tried List<String> and that is the problem.

Read this section of the docs: https://github.com/intuit/karate#type-conversion

Another tip, you can try a "cast" to ensure it is in the JSON array form you need, so if you are too lazy to form properly nested Map-s, just return a raw JSON string from Java, and the below line will convert it correctly.

* json foobar = foobar

Upvotes: 2

Related Questions