There are several questions around this on StackOverflow, but I believe this case to be different. I'm using Java 11 and Mockito 2.11.0.
Here's a minimal JUnit 4 test case demonstrating my problem:
@Test
public void shouldAllowMocking() throws Exception {
ObjectMapper objectMapper = mock(ObjectMapper.class);
when(objectMapper.readValue(anyString(), any(Class.class))).thenThrow(new IOException("the-message"));
}
I'm mocking the behaviour of Jackson's ObjectMapper's readValue(String content, Class<T> valueType) method - documentation here - and the documentation shows that that method can throw an IOException. So why does Mockito report that it is invalid for me to mock throwing such an exception?
Interestingly, if I change the behaviour to throw a JsonParseException, which can also be thrown by that method, then Mockito doesn't complain.