I have function like following to generate an object of provided type from any payload:
public static <T> Optional<T> generateObject(String payloadJson, Class<T> type) {
T objectPayload = null;
try {
objectPayload = objectMapper.readValue(payloadJson, type);
}catch (IOException e) {
log.info(e.getMessage());
}
return Optional.ofNullable(objectPayload);
}
Had written the test case as follows
@Test
void generateObject() throws Exception {
Mockito.when( eventUtil.generateObject("a", Mockito.any(Class.class)) ).thenReturn( Optional.of("") );
Mockito.<Optional<Object>>when( eventUtil.generateObject("b", Mockito.any(Class.class)) ).thenReturn( Optional.of("") );
Optional<Object> o2 = eventUtil.generateObject("b", Mockito.any(Class.class));
assertEquals( "lol", o2.get() );
Mockito.verify(eventUtil);
}
The test execution is failing on Mockito.when statement with
java.lang.IllegalArgumentException: Unrecognized Type: [null]
**Note: I have tried providing null, another model class and even the parent class itself with thenReturn( Optional.of()) with no luck. Please point me to the correct direction in this case.