Mockito: "Checked exception is invalid for this method" when throwing specified checked exception

huangapple go评论66阅读模式
英文:

Mockito: "Checked exception is invalid for this method" when throwing specified checked exception

问题

以下是翻译好的部分:

有关这个问题,在StackOverflow上有几个类似的问题,但我认为这种情况不同。我正在使用Java 11和Mockito 2.11.0。

这是一个演示我的问题的最小 JUnit 4 测试案例:

@Test
public void shouldAllowMocking() throws Exception {
  ObjectMapper objectMapper = mock(ObjectMapper.class);
  when(objectMapper.readValue(anyString(), any(Class.class))).thenThrow(new IOException("the-message"));
}

我正在模拟Jackson的ObjectMapper的readValue(String content, Class<T> valueType)方法的行为 - 文档在这里 - 并且文档显示该方法可以抛出IOException。那么为什么Mockito会报告我无法模拟抛出这种异常呢?

有趣的是,如果我将行为更改为抛出JsonParseException,这个方法也可以抛出,那么Mockito就不会抱怨。

英文:

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(&quot;the-message&quot;));
}

I'm mocking the behaviour of Jackson's ObjectMapper's readValue(String content, Class&lt;T&gt; 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.

答案1

得分: 4

自版本 2.10 起,Jackson 移除了 IOException,这是代码的一部分:

@SuppressWarnings("unchecked")
public <T> T readValue(String content, JavaType valueType)
    throws JsonProcessingException, JsonMappingException
{
    _assertNotNull("content", content);
    try { // 自2.10版本开始,根据[databind#1675]移除了"impossible" IOException
        return (T) _readMapAndClose(_jsonFactory.createParser(content), valueType);
    } catch (JsonProcessingException e) {
        throw e;
    } catch (IOException e) { // 实际上不应该发生,但需要声明
        throw JsonMappingException.fromUnexpectedIOE(e);
    }
}

你提供的链接指向 jakson-databind 2.7,所以我假设你在查看错误的文档。

英文:

Since version 2.10 Jackson removed the IOException, this is the portion of the code:

@SuppressWarnings(&quot;unchecked&quot;)
public &lt;T&gt; T readValue(String content, JavaType valueType)
    throws JsonProcessingException, JsonMappingException
{
    _assertNotNull(&quot;content&quot;, content);
    try { // since 2.10 remove &quot;impossible&quot; IOException as per [databind#1675]
        return (T) _readMapAndClose(_jsonFactory.createParser(content), valueType);
    } catch (JsonProcessingException e) {
        throw e;
    } catch (IOException e) { // shouldn&#39;t really happen but being declared need to
        throw JsonMappingException.fromUnexpectedIOE(e);
    }
} 

The link you included in the question points to the jakson-databind 2.7 so I assume you are checking the wrong doc.

答案2

得分: 1

这是因为我无意中更新了我的jackson版本,较新的版本(2.11.x)不再抛出IOException

英文:

This turns out to be because I'd unintentionally updated my jackson version as well, and the newer versions (2.11.x) don't throw IOException any more.

huangapple
  • 本文由 发表于 2020年8月3日 17:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63226578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定