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

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

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

问题

以下是翻译好的部分:

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

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

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

我正在模拟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:

  1. @Test
  2. public void shouldAllowMocking() throws Exception {
  3. ObjectMapper objectMapper = mock(ObjectMapper.class);
  4. when(objectMapper.readValue(anyString(), any(Class.class))).thenThrow(new IOException(&quot;the-message&quot;));
  5. }

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,这是代码的一部分:

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

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

英文:

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

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

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:

确定