testcontainers/testcontainers-java · error · RuntimeException
Failed to convert arguments into json
Error message
Failed to convert arguments into json: ${e.getMessage()} What it means
RabbitMQContainer.toJson serializes a Map of queue/exchange/binding/policy arguments to JSON with Jackson before sending them to the RabbitMQ HTTP management API. If Jackson cannot serialize the map (JsonProcessingException), it wraps it in a RuntimeException with this message. It almost always means one of the argument values is not JSON-serializable by the default ObjectMapper.
Solutions
- Inspect the arguments map passed to withQueue/withExchange/withBinding/withPolicy/withOperatorPolicy and remove or replace values that are not plain JSON types (String, Number, Boolean, List, Map).
- Ensure numeric arguments are standard types like Integer/Long, not exotic Number subclasses or wrapper objects.
- Enable ObjectMapper FAIL_ON_EMPTY_BEANS awareness: if a value is a complex bean, convert it yourself to a Map/List before passing it.
- Wrap the call in try/catch RuntimeException and log the arguments map to identify the offending entry.
- If a value must be a custom object, serialize it first (new ObjectMapper().writeValueAsString(value)) and pass the resulting JSON-friendly structure.
Example fix
// before
Map<String, Object> args = new HashMap<>();
args.put("x-message-ttl", Duration.ofSeconds(30)); // not a plain number
container.withQueue("q", args);
// after
Map<String, Object> args = new HashMap<>();
args.put("x-message-ttl", 30_000); // Integer/Long serializes fine
container.withQueue("q", args); Defensive patterns
Strategy: validation
Validate before calling
static boolean isJsonSerializable(Object v) {
try { new ObjectMapper().writeValueAsString(v); return true; }
catch (JsonProcessingException e) { return false; }
}
// assert args.values().stream().allMatch(MyContainer::isJsonSerializable); Type guard
static boolean isPlainJsonType(Object v) {
return v == null || v instanceof String || v instanceof Number || v instanceof Boolean
|| v instanceof Map || v instanceof List;
} Try / catch
try {
container.withQueue("q", args);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().contains("Failed to convert arguments into json")) {
throw new IllegalStateException("Non-JSON-serializable argument in map: " + args, e);
}
throw e;
} Prevention
- Restrict argument maps to String/Number/Boolean/List/Map values
- Convert custom option objects to Maps before passing
- Keep numeric options as Integer/Long, not exotic Number types
- Unit-test any helper that builds RabbitMQ argument maps
When it happens
Trigger: Calling withQueue(), withExchange(), withBinding(), withPolicy(), or withOperatorPolicy() with an arguments map containing a value Jackson cannot write, e.g. an arbitrary non-POJO object, an unserializable type, or (rarely) a map with a null key.
Common situations: Passing custom typed option objects (e.g. x-message-ttl as a custom wrapper or BigDecimal with broken config), reusing a Map with mixed/generic Object values containing framework types (OutputStream, lambdas, Hibernate entities), or copy-pasting arguments built for another client library.
Understand the failure class
Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.
Related errors
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/0ff9f6f5eab13538.
Report an issue: GitHub.
Appendix: source
Thrown at modules/rabbitmq/src/main/java/org/testcontainers/containers/RabbitMQContainer.java:721
}
/**
* Overwrites the default RabbitMQ configuration file with the supplied one.
*
* @param rabbitMQConf The rabbitmq.config file to use (in erlang format)
* @return This container.
*/
public RabbitMQContainer withRabbitMQConfigErlang(MountableFile rabbitMQConf) {
withEnv("RABBITMQ_CONFIG_FILE", "/etc/rabbitmq/rabbitmq-custom.config");
return withCopyFileToContainer(rabbitMQConf, "/etc/rabbitmq/rabbitmq-custom.config");
}
@NotNull
private String toJson(Map<String, Object> arguments) {
try {
return new ObjectMapper().writeValueAsString(arguments);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to convert arguments into json: " + e.getMessage(), e);
}
}
}
View on GitHub (pinned to 8e549514e3)