spring-ai
spring-ai copied to clipboard
test: Add comprehensive test coverage for JsonParser
trafficstars
Summary
This PR adds comprehensive test coverage for the JsonParser utility class, focusing on error handling, edge cases, and previously untested functionality.
Changes
Error Handling Tests (3 tests)
shouldThrowExceptionWhenJsonIsMalformed- ValidatesIllegalStateExceptionis thrown for malformed JSON withfromJson(Class)shouldThrowExceptionWhenJsonIsMalformedWithType- Validates error handling forfromJson(Type)overloadshouldThrowExceptionWhenJsonIsMalformedWithTypeReference- Validates error handling forfromJson(TypeReference)overload
Generic Collections Support (2 tests)
fromJsonToListWithTypeReference- Tests deserialization of JSON arrays to typedList<Integer>fromJsonToMapWithTypeReference- Tests deserialization of JSON objects to typedMap<String, String>
Null Parameter Validation (2 tests)
shouldThrowExceptionWhenValueIsNullInToTypedObject- Ensures null value parameter throwsIllegalArgumentExceptionshouldThrowExceptionWhenTypeIsNullInToTypedObject- Ensures null type parameter throwsIllegalArgumentException
Primitive Type Handling (3 tests)
fromObjectToPrimitiveInt- Tests conversion to primitiveinttypefromObjectToPrimitiveLong- Tests conversion to primitivelongtypefromObjectToPrimitiveBoolean- Tests conversion to primitivebooleantype
Edge Cases (4 tests)
shouldThrowExceptionWhenConversionFails- ValidatesNumberFormatExceptionfor invalid number formatsfromEmptyJsonObject- Tests parsing empty JSON object{}fromEmptyJsonArray- Tests parsing empty JSON array[]toTypedObjectWithNonParsableJsonString- Tests fallback behavior when JSON string parsing fails
Testing
All new tests pass and improve overall test coverage for the JsonParser class. The tests validate:
- Error conditions are properly handled
- Generic type deserialization works correctly
- Null parameters are validated
- Primitive types are properly resolved
- Edge cases behave as expected