javajunitjacksonmockito

Junit is throwing error with the com.fasterxml.jackson.databind.exc.InvalidDefinitionException


Tried with the changes in object but still getting the error while running test case. Let me know if someone can help on this. The method is below:

public String processResponseAudit(Object inquireClaimFilterResponse, String userId, String spanId,
            String corelationId) throws UnknownHostException, JsonProcessingException {
        String auditStatus = "";
        ObjectMapper objectMapper = new ObjectMapper();
        String responseJson = objectMapper.writeValueAsString(inquireClaimFilterResponse);
        LocalDateTime myDateObj = LocalDateTime.now();
        DateTimeFormatter dateStampObj = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
        String dateStamp = myDateObj.format(dateStampObj);
        LocalDateTime localDateTime = LocalDateTime.parse(dateStamp, dateStampObj);
        MapSqlParameterSource auditParams = new MapSqlParameterSource();
        auditParams.addValue(RSPNS_TIMESTAMP, Timestamp.valueOf(myDateObj));
        auditParams.addValue(RSPNS_BODY, responseJson, java.sql.Types.OTHER);
        auditParams.addValue(MODIFIED_BY, userId);
        auditParams.addValue(MODIFIED_DATE, localDateTime, java.sql.Types.TIMESTAMP);
        auditParams.addValue(CORLTN_ID, corelationId);
        auditParams.addValue(SPAN_ID, spanId);
        int auditResponse = selfServiceDao.auditApiResponseData(auditParams);
        if (auditResponse == 1) {
            auditStatus = "Audit Data Inserted Successfully";
        } else if (auditResponse == 0) {
            auditStatus = "Audit Data Insertion Failed";
        }
        return auditStatus;
    }}

the Junit for the method is as follows:

 @Test
    public void testProcessResponseAudit_Success() throws JsonProcessingException, UnknownHostException {
    when(selfServiceDao.auditApiResponseData(any(MapSqlParameterSource.class))).thenReturn(1);
        
        Object inquireClaimFilterRequest = new Object(); 

        String result = selfServiceCoreClaimImpl.processResponseAudit(inquireClaimFilterRequest, "testUser", UUID.randomUUID().toString(),
                UUID.randomUUID().toString());
        assertEquals("Audit Data Inserted Successfully", result);
}

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1308)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:414)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:53)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:30)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:479)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:318)
    at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4719)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3964)
    at com.impl.SelfServiceCoreClaimImpl.processResponseAudit(SelfServiceCoreClaimImpl.java:257)
    at com.impl.SelfServiceCoreClaimImplTest.testProcessResponseAudit_Success(SelfServiceCoreClaimImplTest.java:132)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Solution

  • The error comes from this line:

    String responseJson = objectMapper.writeValueAsString(inquireClaimFilterResponse);

    The Object passed is an empty Java Object instantiated inside the test:

            Object inquireClaimFilterRequest = new Object(); 
    

    The exception can be removed by disabling the SerializationFeature.FAIL_ON_EMPTY_BEANS which will return an empty serialization for the Object {}.

         //Import to use
        import com.fasterxml.jackson.databind.SerializationFeature;
    
        // Disable FAIL_ON_EMPTY_BEANS to avoid exception for empty objects
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    

    This what the parenthesis in the com.fasterxml.jackson.databind.exc.InvalidDefinitionException exception message refers to

    (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

    Another point to consider is that objects that will be passed in the Object inquireClaimFilterResponse parameter in the method processResponseAudit are unlikely to be plain Object instances. Most probably the parameters passed in as arguments will be other Class instances that will be more relevant to the application business. It could make sense if tests were created using these types as well.