title: Mockito Matcher for a TypeReference
body: <p>I am trying to write a Java unit test using Mockito but am having trouble to get a matcher to work.</p>
  
  <p>I want to test the following class</p>
  
  <p><strong>CustomService.java</strong></p>
  
  <pre><code>public class CustomService {
  
  private final ApplicationProperties props;
  
  public CustomService(ApplicationProperties props){
      this.props = props;
  }
  
  private final ObjectMapper mapper = new ObjectMapper();
  public void method(JsonNode message) throws CustomException {
      try {
          List&lt;String&gt; actions = mapper.readValue(message.get("actions").toString(), mapper.getTypeFactory().constructCollectionType(List.class, String.class));
          System.out.println(actions);
      } catch (IOException ex){
          throw new CustomException(ex);
      }
  }
  }
  </code></pre>
  
  <p>I have a CustomException class</p>
  
  <p><strong>CustomExcepton.java</strong></p>
  
  <pre><code>@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
  public class CustomException extends Exception {
  
  public CustomException(Throwable cause) {
      super(cause);
      }
  
  }
  </code></pre>
  
  <p>I want to test that the CustomException is thrown.  I am using Mockito.  I have tried the below but the <strong>given</strong> statement doesn't seem to pick up the (readValue) line of code in the method </p>
  
  <p><strong>CustomServiceTest.java</strong></p>
  
  <pre><code>public class CustomServiceTest {
  
  private final ApplicationProperties props = mock(ApplicationProperties.class);
  private final CustomService customService = new CustomService(props);
  private static final ObjectMapper objectMapper = new ObjectMapper();
  
  @Test
  public void CustomExceptionIsThrown() throws Exception {
      ObjectMapper mapper = mock(ObjectMapper.class);
      given(mapper.readValue(anyString(), any(TypeReference.class))).willThrow(new IOException("This is a test"));
      String json = "{\"actions\":[\"ac1\",\"ac2\",\"ac3\",\"ac4\"]}";
      JsonNode d = objectMapper.readTree(json);
      assertThrows(CustomException.class, () -&gt;
              customService.method(d));
      }
  }
  </code></pre>
  
  <p>I get the following error when I run the test</p>
  
  <pre><code>Expected exception.CustomException to be thrown, but nothing was thrown..
  </code></pre>
  
  <p>Help would be appreciated.</p>