2

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;
    }
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • not sure about karate, but to print contents of array use `Arrays.toString` like `System.out.println(Arrays.toString(ruleNameArray));` – sanjeevRm Jul 27 '21 at 05:45

1 Answers1

0

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • When I read the part you wrote "string arrays are not being converted to JSON arrays properly", I realized that maybe that variable definition needed to be a JSON definition instead. I changed "* def ruleNameArray = objUtility.createRuleNameArray(newRuleBase, maxDiffParse)" to "* json ruleNameArray = objUtility.createRuleNameArray(newRuleBase, maxDiffParse)" and the problem was solved! Thank you! – Allison Crenshaw Jul 27 '21 at 15:49
  • @AllisonCrenshaw no problem. I do recommend using a `List`, in this case the parser is "forgiving" the lack of quotes around the strings, you may not be so lucky in other use-cases – Peter Thomas Jul 27 '21 at 15:51
  • @AllisonCrenshaw well. first read this ! https://stackoverflow.com/a/52078427/143475 and then, if you still need to, follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Jul 29 '21 at 02:05