karatelabs/karate · error · RuntimeException

sizeOf() needs one argument

Error message

sizeOf() needs one argument

What it means

sizeOf() is a Karate JS utility returning the number of elements in a List or Map (and similar sized values). It throws this error when called with zero arguments, since there is no collection to measure.

Solutions

  1. Pass the collection: sizeOf(myList) or sizeOf(myMap).
  2. Confirm the variable is defined before the assertion.
  3. If it may be null/undefined, default it first: sizeOf(items || []).

Example fix

// before
match sizeOf() == 3
// after
match sizeOf(response.items) == 3
Defensive patterns

Strategy: validation

Validate before calling

def n = collection ? sizeOf(collection) : 0

Prevention

When it happens

Trigger: karate.sizeOf() with no arguments, e.g. sizeOf() instead of sizeOf(myList).

Common situations: Asserting a response length and omitting the argument; the collection variable was removed in a refactor; copying a match expression without filling in the value.

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


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/47252cb4c7e1b4d5. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:467

    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) {
                return ((List<?>) obj).size();
            } else if (obj instanceof Map) {
                return ((Map<?, ?>) obj).size();
            } else if (obj instanceof String) {
                return ((String) obj).length();
            }
            return 0;
        };
    }

    static JavaInvokable sort() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("sort() needs at least one argument: the list to sort");
            }

View on GitHub (pinned to a22eb90246)