karatelabs/karate · error · RuntimeException
toBean() needs two arguments: object and class name
Error message
toBean() needs two arguments: object and class name
What it means
karate.toBean() converts a JS/Map object to an instance of a named Java class by serializing it to JSON and deserializing with Json.fromJson into the target class. The library requires exactly this pair — the object and the class name string — so it throws when fewer than two arguments are supplied. Without both, conversion is impossible since neither the data nor the target type is known.
Solutions
- Supply both arguments: karate.toBean(obj, 'com.example.MyClass') with the fully qualified class name as a string.
- Ensure the first argument is the object (Map) and the second is the class name string — order matters.
- If the object may be null, still pass it explicitly so the call has two arguments.
Example fix
// before var user = karate.toBean(response) // after var user = karate.toBean(response, 'com.example.User')
Defensive patterns
Strategy: validation
Validate before calling
if (obj == null || typeof className !== 'string' || className.indexOf('.') < 0) { throw new Error('toBean(obj, fullyQualifiedClassName) requires an object and a class name string') } Type guard
function canToBean(obj, className) { return obj != null && typeof className === 'string' && className.length > 0 } Try / catch
try { var bean = karate.toBean(obj, 'com.example.User') } catch (e) { if (('' + e).indexOf('needs two arguments') >= 0) { karate.logger.warn('toBean misused: need object + class name'); bean = null } throw e } Prevention
- Always pass the fully qualified class name as a string
- Keep object-first, class-name-second argument order
- Wrap toBean calls in a small helper function so arity mistakes surface in one place
When it happens
Trigger: karate.toBean() with zero or one argument, e.g. karate.toBean(someMap) forgetting the class name, or karate.toBean('com.example.User') forgetting the object.
Common situations: Bridging JS objects to Java POJOs for Java interop in tests; copying old call signatures where the class name was previously inferred; typos after refactoring the helper's arity.
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
- read() needs at least one argument
- sysenv() needs the environment-variable name
- sysprop() needs the property name
- readAsBytes() needs at least one argument
- get() needs at least one argument
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/bc6768a44ec48073.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:524
@SuppressWarnings("unchecked")
private static int compareKeys(Object a, Object b) {
if (a == null || b == null) {
return a == b ? 0 : (a == null ? -1 : 1);
}
if (a instanceof Number && b instanceof Number) {
return Double.compare(((Number) a).doubleValue(), ((Number) b).doubleValue());
}
if (a instanceof Comparable && a.getClass().isInstance(b)) {
return ((Comparable<Object>) a).compareTo(b);
}
// last resort for mixed or non-comparable keys, at least the order is stable
return String.valueOf(a).compareTo(String.valueOf(b));
}
static JavaInvokable toBean() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("toBean() needs two arguments: object and class name");
}
Object obj = args[0];
String className = args[1].toString();
// Convert to JSON string and deserialize to the target class
String jsonString = Json.of(obj).toString();
return Json.fromJson(jsonString, className);
};
}
/**
* Convert a list of maps to CSV string.
* Usage: karate.toCsv([{a:1,b:2},{a:3,b:4}]) => "a,b\n1,2\n3,4\n"
*/
@SuppressWarnings("unchecked")
static JavaInvokable toCsv() {
return args -> {
if (args.length == 0 || args[0] == null) {
return "";View on GitHub (pinned to a22eb90246)