Why is my Java function in my Karate feature returning a String instead of an array?

I am new to software testing and working in a Karate environment. Currently I am working on a story where I have to test that the maximum number of database entries is working -- that when the max number of entries is reached (20), the program returns a 400 error and a message that the max entries have been reached.

The scenario that we have in Karate passes and is working fine when there are already 20 entries but that is not always the case as different tests are creating and deleting entries as they're being worked on and run. Currently I have code set up to calculate the number of rules needed to max out the DB (maxTotalRules-ruleCount) and have that parsed to an int. Then I pass that and a random string to a Java method. The java method then uses a for loop to make an array of names (abcd_1, abcd_2, etc.), however many required by the DB in its current state, and then returns the array.

So I have my variable definition with the call and then logging the resulting variable to see that it contains what it should:

* def ruleNameArray = objUtility.createRuleNameArray(newRuleBase, maxDiffParse)
* karate.log(ruleNameArray)

My problem though is that arrays in Karate normally print like this:

[
  1,
  2
]

The returned array is printing like this:

Automation_Rule-botrrxyo_1 Automation_Rule-botrrxyo_2 Automation_Rule-botrrxyo_3

It is printing all in one line, almost like it is one big string, as opposed to coming out like an array. And when I try to use that array to build a request body to be passed to my API to add the new database entries, I am getting the following error message: "java.lang.RuntimeException: not map-like or list-like"

This may be a long explanation, but essentially my question is why is my Java method return not being received as an array? And how can I fix this?

If it is needed for context/to make sure my method is written properly, here is the Java method:

public String[] createRuleNameArray(String nameBase, int numOfNamesNeeded) {
        String[] ruleNameArray;
        ruleNameArray = new String[numOfNamesNeeded];

        for(int i=0; i < numOfNamesNeeded; i++) {
            ruleNameArray[i] = nameBase+(i+1);
            System.out.println(ruleNameArray[i]);
        }
        return ruleNameArray;
    }

1 answer

  • answered 2021-07-27 05:42 Peter Thomas

    I guess you are using the 1.0.1 version and there may be a limitation in that version where Java string arrays are not being converted to JSON arrays properly.

    The solution that should work right away is - please can you try returning List<String> from the Java createRuleNameArray() function - and then things should work.

    It would be also very helpful if you can try Karate 1.1.0.RC5 and let us know whether that fixes the above limitation.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum