title: How to test exception is thrown with Junit
body: <p>I would like to know how I can write a unit test to get the catch block covered for the following method. The FOM.create(data) is a static method.</p>
  
  <pre><code>  public String getValue(Data data) {
        try {
            return FOM.create(data);
        } catch (UnsupportedEncodingException e) {
            log.error("An error occured while creating data", e);
            throw new IllegalStateException(e);
        }
    }
  </code></pre>
  
  <p>Currently this is my unit test but it doesn't hit the catch block:</p>
  
  <pre><code>@Test (expected = UnsupportedEncodingException.class)
  public void shouldThrowUnsupportedEncodingException() {
      doThrow(UnsupportedEncodingException.class).when(dataService).getUpdatedJWTToken(any(Data.class));
      try {
          dataService.getValue(data);
      }catch (IllegalStateException e) {
          verify(log).error(eq("An error occured while creating data"), any(UnsupportedEncodingException.class));
          throw e;
      }
  }
  </code></pre>