spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

test: Add comprehensive test coverage for JsonParser

Open alxkm opened this issue 3 weeks ago • 1 comments
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 - Validates IllegalStateException is thrown for malformed JSON with fromJson(Class)
  • shouldThrowExceptionWhenJsonIsMalformedWithType - Validates error handling for fromJson(Type) overload
  • shouldThrowExceptionWhenJsonIsMalformedWithTypeReference - Validates error handling for fromJson(TypeReference) overload

Generic Collections Support (2 tests)

  • fromJsonToListWithTypeReference - Tests deserialization of JSON arrays to typed List<Integer>
  • fromJsonToMapWithTypeReference - Tests deserialization of JSON objects to typed Map<String, String>

Null Parameter Validation (2 tests)

  • shouldThrowExceptionWhenValueIsNullInToTypedObject - Ensures null value parameter throws IllegalArgumentException
  • shouldThrowExceptionWhenTypeIsNullInToTypedObject - Ensures null type parameter throws IllegalArgumentException

Primitive Type Handling (3 tests)

  • fromObjectToPrimitiveInt - Tests conversion to primitive int type
  • fromObjectToPrimitiveLong - Tests conversion to primitive long type
  • fromObjectToPrimitiveBoolean - Tests conversion to primitive boolean type

Edge Cases (4 tests)

  • shouldThrowExceptionWhenConversionFails - Validates NumberFormatException for invalid number formats
  • fromEmptyJsonObject - 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

alxkm avatar Oct 30 '25 21:10 alxkm