{"id":"3ccf4ba0490a5116","repo":"junit-team/junit5","slug":"argument-at-index-d-with-value-s-and-type","errorCode":null,"errorMessage":"Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].","messagePattern":"Argument at index \\[(.+?)\\] with value \\[(.+?)\\] and type \\[(.+?)\\] could not be converted or cast to type \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ArgumentAccessException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java","lineNumber":81,"sourceCode":"\tpublic @Nullable Object get(int index) {\n\t\tPreconditions.condition(index >= 0 && index < this.arguments.length,\n\t\t\t() -> \"index must be >= 0 and < %d\".formatted(this.arguments.length));\n\t\treturn this.arguments[index];\n\t}\n\n\t@Override\n\tpublic <T> @Nullable T get(int index, Class<T> requiredType) {\n\t\tPreconditions.notNull(requiredType, \"requiredType must not be null\");\n\t\tObject value = get(index);\n\t\ttry {\n\t\t\tObject convertedValue = converter.apply(value, requiredType);\n\t\t\treturn requiredType.cast(convertedValue);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tString message = \"Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].\".formatted(\n\t\t\t\tindex, value, ClassUtils.nullSafeToString(value == null ? null : value.getClass()),\n\t\t\t\trequiredType.getName());\n\t\t\tthrow new ArgumentAccessException(message, ex);\n\t\t}\n\t}\n\n\t@Override\n\tpublic @Nullable Character getCharacter(int index) {\n\t\treturn get(index, Character.class);\n\t}\n\n\t@Override\n\tpublic @Nullable Boolean getBoolean(int index) {\n\t\treturn get(index, Boolean.class);\n\t}\n\n\t@Override\n\tpublic @Nullable Byte getByte(int index) {\n\t\treturn get(index, Byte.class);\n\t}\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java#L63-L99","documentation":"Thrown by DefaultArgumentsAccessor.get(int index, Class<T> requiredType) when the argument at the given index cannot be converted to the requested type via DefaultArgumentConverter, or when the converted value cannot be cast to the required type. This is an ArgumentAccessException that wraps the underlying conversion/cast failure.","triggerScenarios":"Calling accessor.get(index, SomeType.class) on an ArgumentsAccessor where the argument at that index is incompatible with SomeType — e.g., accessor.get(0, Integer.class) when the argument is a non-numeric string 'abc', or accessor.get(0, LocalDate.class) when the string doesn't parse as a date.","commonSituations":"Using an ArgumentsAccessor (via @AggregateWith or a parameter of type ArgumentsAccessor) to flexibly access arguments, but requesting a type that doesn't match the actual argument. CSV data where a column sometimes contains non-numeric or non-parseable values. Assuming a column is always a number when it sometimes contains sentinel strings.","solutions":["Inspect the error message which includes the actual value, its type, and the requested type","Ensure the source data (CSV, method source) provides values compatible with the requested type at that index","Use accessor.getString(index) for raw access and parse manually if the type is uncertain","Add a @ConvertWith converter for complex type transformations"],"exampleFix":"// before\n@ParameterizedTest\n@CsvSource({ \"abc, 2\" })\nvoid test(ArgumentsAccessor accessor) {\n    int a = accessor.getInteger(0); // 'abc' cannot convert to int\n}\n\n// after\n@ParameterizedTest\n@CsvSource({ \"1, 2\" })\nvoid test(ArgumentsAccessor accessor) {\n    int a = accessor.getInteger(0);\n}","handlingStrategy":"try-catch","validationCode":"// Check argument type compatibility before calling accessor.get(index, type):\nObject raw = accessor.get(index);\nif (raw != null && !requiredType.isAssignableFrom(raw.getClass())\n        && !(raw instanceof String)) {\n    throw new IllegalStateException(\n        \"Argument at \" + index + \" is \" + raw.getClass() + \", not convertible to \" + requiredType);\n}","typeGuard":"// Narrow the type before accessing:\nObject raw = accessor.get(index);\nif (raw instanceof Integer i) {\n    int value = i;\n} else {\n    // handle non-integer case\n}","tryCatchPattern":"try {\n    Integer value = accessor.getInteger(0);\n} catch (ArgumentAccessException e) {\n    // handle: log, skip, or provide default\n    throw new AssertionFailedError(\"Expected integer at index 0: \" + e.getMessage());\n}","preventionTips":["Ensure CSV/source data columns are compatible with the types requested via accessor.get(index, type)","Use accessor.get(index) (untyped) and check the runtime type before converting if data is heterogeneous","Validate source data before running tests, especially for CSV files with mixed content","Consider accessor.getString(index) and parse manually for maximum control"],"tags":["arguments-accessor","type-conversion","parameterized-test","type-mismatch","junit-jupiter-params"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}