Getting org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method! Even though I am throwing correct exception. I am trying to test this class DecileConfigurationJsonConverter. In the test I am testing the exception throw scene while reading DB data. Its throws IOException if it cant read DB data and in the mock I did throw the exact same Exception yet I got the above error.
public class DecileConfigurationJsonConverter implement AttributeConverter<List<DecileParameter>,String>{
private static final ObjectMapper objectMapper = new ObjectMapper();
@Override
public List<DecileParameter> convertToEntityAttribute(String dbData) {
if(dbData==null)
return null;
List<DecileParameter> dep = null;
try
{
dep = objectMapper.readValue(dbData,
new TypeReference<List<DecileParameter>>() {});
}
catch (final IOException e)
{
return null;
}
return dep;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("dev-test")
@TestPropertySource(locations = "classpath:application-dev-test.yml")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UtilPackageTestMore {
@Mock
ObjectMapper objectMapper;
private static void setFinalStaticField(Class<?> clazz, String fieldName, Object value)
throws ReflectiveOperationException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, value);
}
@Test
public void TestDecileConfigurationJsonConverterExceptions() throws Exception
{
DecileConfigurationJsonConverter dc= new DecileConfigurationJsonConverter();
ObjectMapper objecmapper = Mockito.mock(ObjectMapper.class);
setFinalStaticField(DecileConfigurationJsonConverter.class, "objectMapper", objecmapper);
List<DecileParameter> attribute = new ArrayList<>();
attribute.add(new DecileParameter("1","2","3","4","5"));
Mockito.when(objecmapper.writeValueAsString(attribute)).thenThrow(JsonProcessingException.class);
String res = dc.convertToDatabaseColumn(attribute);
assertNull(res);
//here is the exception got thrown
Mockito.when(objecmapper.readValue("db data",new TypeReference<List<DecileParameter>>() {})).thenThrow(java.io.IOException.class);
List<DecileParameter> convertToEntityAttribute = dc.convertToEntityAttribute("db data");
assertNull(convertToEntityAttribute);
}
You must throw an Exception
, but you are throwing a Class<Exception>
. JsonProcessingException.class
returns a Class<Exception>
instance. new JsonProcessingException()
returns a new Exception
instance. You can only throw the latter.
Mockito.when(objecmapper.writeValueAsString(attribute))
.thenThrow(new JsonProcessingException());
But in fact you don't need to mock anything at all here. Try to avoid Reflection. Keep your tests simple and don't couple them to your implementation.
public class DecileConfigurationJsonConverter
implements AttributeConverter<List<DecileParameter>, String> {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Override
public List<DecileParameter> convertToEntityAttribute(final String dbData) {
if (dbData==null) return null;
try
{
return OBJECT_MAPPER.readValue(dbData,
new TypeReference<List<DecileParameter>>() {});
}
catch (final IOException e)
{
return null;
}
}
}
public class UtilPackageTestMore {
@Test
public void TestDecileConfigurationJsonConverterExceptions() throws Exception
{
final DecileConfigurationJsonConverter dc = new DecileConfigurationJsonConverter();
final List<DecileParameter> convertToEntityAttribute = dc.convertToEntityAttribute("INVALID_JSON"); // value can be anything that cannot be deserialized
assertNull(convertToEntityAttribute);
}
}