{"record":{"id":"aab76cebc928d7e5","repo":"karatelabs/karate","slug":"assert-failed","errorCode":null,"errorMessage":"assert failed: ","messagePattern":"assert failed: ","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/StepExecutor.java","lineNumber":1902,"sourceCode":"        return value;\n    }\n\n    /**\n     * True for an expression like {@code ({ a: 1 })} or {@code ([1, 2])} — a JSON literal\n     * the user forced through JS evaluation by wrapping it in round brackets.\n     */\n    private static boolean isParenWrappedJson(String expr) {\n        if (expr.length() < 3 || expr.charAt(0) != '(' || expr.charAt(expr.length() - 1) != ')') {\n            return false;\n        }\n        return StringUtils.looksLikeJson(expr.substring(1, expr.length() - 1).trim());\n    }\n\n    private void executeAssert(Step step) {\n        Object result = runtime.eval(step.getText());\n        if (result instanceof Boolean b) {\n            if (!b) {\n                throw new AssertionError(withCommentLabel(step, \"assert failed: \" + step.getText()));\n            }\n        } else {\n            throw new RuntimeException(\"assert expression must return boolean: \" + step.getText());\n        }\n    }\n\n    private static final LogContext.LogWriter SCENARIO_LOG = LogContext.with(LogContext.SCENARIO_LOGGER);\n\n    private void executePrint(Step step) {\n        // Wrap in array to handle comma-separated expressions like: print 'foo', 'bar'\n        // Without wrapping, JS comma operator would return only the last value\n        Object value = runtime.eval(\"[\" + step.getText() + \"]\");\n        if (value instanceof List<?> list) {\n            StringBuilder sb = new StringBuilder();\n            for (int i = 0; i < list.size(); i++) {\n                if (i > 0) {\n                    sb.append(' ');\n                }","sourceCodeStart":1884,"sourceCodeEnd":1920,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/StepExecutor.java#L1884-L1920","documentation":"Karate's 'assert' step evaluates its text as an expression; if the resulting boolean is false, an AssertionError labeled 'assert failed: <expression>' is thrown. This is an intentional assertion failure, not a bug — the JS expression evaluated to false.","triggerScenarios":"An 'assert' step in a feature whose expression returns false, e.g. 'assert responseStatus == 200' after a non-200 response, or 'assert myVar.length == 3' with a different length.","commonSituations":"API regression caught by the test (wrong status code, wrong computed value); type coercion surprises making the expression false (string '5' vs number 5); evaluating against undefined variables which yield false-y comparisons.","solutions":["Print the values involved (print responseStatus, myVar) to see actual vs expected","Fix the expectation if the product behavior is correct, or fix the underlying bug/fixture if not","Watch for type mismatches — compare with explicit conversions when values come from JSON strings","Ensure referenced variables are defined earlier (undefined comparisons often evaluate false)"],"exampleFix":"// before: fails when status is 201\nassert responseStatus == 200\n// after: accept both success codes\nassert responseStatus == 200 || responseStatus == 201","handlingStrategy":"validation","validationCode":"// log operands before asserting on them\nprint('responseStatus:', responseStatus, 'expected:', 200);\n// assert responseStatus == 200","typeGuard":null,"tryCatchPattern":"// AssertionError is intentional; let it propagate and read the labeled expression\ntry { executor.execute(step); } catch (AssertionError e) { report.stepFailed(step, e.getMessage()); throw e; }","preventionTips":["print the compared values before asserting","Beware string vs number comparisons on JSON values","Ensure asserted variables are defined earlier in the scenario","Match actual behavior against specs before 'fixing' the assert"],"tags":["karate","assert","assertion-failed","test"],"backgroundTag":"assertion-failed","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}