title: Mockito checked exception is invalid for this method
body: <p>I'm getting the error <code>Checked exception is invalid for this method</code> when trying to use this mock:</p>
  
  <pre><code>InitialContext mockContext;
  mockContext = mock(InitialContext.class);
  when((DataSource) mockContext.lookup("java:comp/env/jdbc/foo")).thenThrow(new ConnectionFactoryException("test")); // &lt;-- Fails on this line
  
  @Test
  public void shouldThrowExceptionIfDataSourceDoesNotExist() throws ConnectionFactoryException {
      assertThatExceptionOfType(ConnectionFactoryException.class)
          .isThrownBy(() -&gt; { new DataSourceFactory(mockContext).getDataSource("foo"); })
          .withMessage("Unable to find jdbc/foo");
  }
  </code></pre>
  
  <p>Class I'm trying to test:</p>
  
  <pre><code>public class DataSourceFactory {
  
      // Dependencies to be injected
      private InitialContext context;
  
      public DataSourceFactory(InitialContext context) throws ConnectionFactoryException {
  
          if(context == null) {
              throw new ConnectionFactoryException("context can't be null");
          }
  
          this.context = context;
      }
  
      public DataSource getDataSource(String dataSourceName) throws ConnectionFactoryException {
  
          if(dataSourceName == null) {
              throw new ConnectionFactoryException("dataSourceName can't be null");
          }
  
          // Ensure we have the correct syntax for the datasource name
          if(!dataSourceName.startsWith("jdbc/")) {
              dataSourceName = "jdbc/" + dataSourceName;
          }
  
          try {
              System.out.println("java:comp/env/" + dataSourceName);
              DataSource dataSource = (DataSource) context.lookup("java:comp/env/" + dataSourceName);
              return dataSource;
          } catch (NamingException e) {
              throw new ConnectionFactoryException("Unable to find " + dataSourceName, e);
          }
      }
  }
  </code></pre>
  
  <p>I know I've read that I shouldn't mock objects I don't own, but I can't figure out how to test the <code>getDataSource()</code> method without mocking <code>InitialContext</code>.</p>