karatelabs/karate · error · AssertionError

${result.message optionally prefixed with source from…

Error message

${result.message optionally prefixed with source from context}

What it means

This is Karate's expect() assertion failure. Expect.handleResult evaluates the match result and, when it did not pass and throwOnFailure is set, throws an AssertionError whose message is the assertion's own message optionally prefixed by the current source (e.g. script location) from the scenario context.

Solutions

  1. Read the assertion message (and the source prefix) to identify which expect() statement failed and compare actual vs expected values
  2. Fix the expectation or investigate why the actual value differs (call the endpoint/inspect data)
  3. If the assertion is intentionally exploratory, wrap it so failure is non-fatal or use a soft-assert onResult instead of throwing

Example fix

// before
expect(response.name).to.equal('jane') // AssertionError: actual 'John', expected: 'jane'
// after
expect(response.name.toLowerCase()).to.equal('jane')
Defensive patterns

Strategy: try-catch

Validate before calling

// Karate JS
if (response.status == 200 && response.body.items != null) {
  expect(response.body.items.length).to.be.at.least(1);
}

Type guard

function isObject(v) { return v != null && typeof v === 'object'; }

Try / catch

// JS in Karate
try {
  expect(response.name).to.equal('jane');
} catch (e) {
  karate.log('assertion failed: ' + e.message);
  // decide whether to fail the scenario or record softly
}

Prevention

When it happens

Trigger: Any expect(...)... assertion that evaluates to a failed Result while throwOnFailure is true, e.g. expect(foo).to.equal('bar') when foo != 'bar', expect(list).to.contain(x) when the element is missing, or expect(obj).to.have.keys(...) mismatch.

Common situations: Failing API assertions in karate feature scripts; response data changed upstream; typos in expected keys or values; type differences between actual and expected (string vs number).

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/match/Expect.java:154

    private void handleResult(Context context, Result result) {
        if (negated) {
            result = result.pass ? Result.fail("expected condition to be false") : Result.PASS;
        }
        // Use context supplier if context is null and supplier is available
        Context effectiveContext = context;
        if (effectiveContext == null && contextSupplier != null) {
            effectiveContext = contextSupplier.get();
        }
        if (onResult != null) {
            onResult.accept(effectiveContext, result);
        }
        if (!result.pass && throwOnFailure) {
            String message = result.message;
            String source = getSourceFromContext(effectiveContext);
            if (source != null) {
                message = source + "\n" + message;
            }
            throw new AssertionError(message);
        }
    }

    private static Result evaluate(Object actual, Match.Type type, Object expected) {
        try (Value value = Match.evaluate(actual, null, null)) {
            return value.is(type, expected);
        }
    }

    private JavaCallable match(Match.Type type) {
        return (context, args) -> {
            Result result = evaluate(subject, type, args[0]);
            handleResult(context, result);
            return null;
        };
    }

    private JavaCallable matchChainable(Match.Type type) {

View on GitHub (pinned to a22eb90246)