karatelabs/karate · error · RuntimeException
repeat() needs two arguments: count and function
Error message
repeat() needs two arguments: count and function
What it means
repeat() is a Karate JS utility that calls a function N times (the function receives the index) and collects results in a List. It throws this error when called with fewer than two arguments, because both the count and the function are mandatory.
Solutions
- Supply both arguments: repeat(count, fn) where fn(i) returns the value for each iteration.
- Ensure the first argument is a number and the second an actual function.
- Verify the count is defined and non-null before the call.
Example fix
// before
def list = repeat(3)
// after
def list = repeat(3, function(i){ return 'item' + i }) Defensive patterns
Strategy: validation
Validate before calling
def list = (count && fn) ? repeat(count, fn) : []
Prevention
- Signature is repeat(count, function(i){...}) — two arguments always, in that order.
- Define the callback function before calling repeat().
- Verify count is a number; define it in a prior step if computed.
When it happens
Trigger: karate.repeat() with zero or one argument, e.g. repeat(5) without the function, or repeat() entirely; a null count variable reducing the bound arguments.
Common situations: Forgetting the callback when translating a for-loop; passing the arguments in the wrong order; the function variable is undefined so the arity check fails.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- map() needs two arguments: list and function
- mapWithKey() needs two arguments: list and key name
- range() needs at least two arguments: start and end
- append() needs at least two arguments
- appendTo() needs at least two arguments: list and item(s)
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/25df9f2a7e3f14c2.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:452
return args -> {
if (args.length == 0) {
throw new RuntimeException("prettyXml() needs one argument");
}
Object obj = args[0];
if (obj instanceof Node) {
return Xml.toString((Node) obj, true);
} else if (obj instanceof String) {
return Xml.toString(Xml.toXmlDoc((String) obj), true);
} else {
throw new RuntimeException("prettyXml() argument must be XML node or string");
}
};
}
static JavaInvokable repeat() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("repeat() needs two arguments: count and function");
}
int count = ((Number) args[0]).intValue();
JavaCallable fn = (JavaCallable) args[1];
List<Object> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
result.add(fn.call(null, new Object[]{i}));
}
return result;
};
}
static JavaInvokable sizeOf() {
return args -> {
if (args.length == 0) {
throw new RuntimeException("sizeOf() needs one argument");
}
Object obj = args[0];
if (obj instanceof List) {View on GitHub (pinned to a22eb90246)