title: How can I mock a void method to throw an exception?
body: <p>I have a structure like this:</p>
  
  <pre><code>public class CacheWrapper {
      private Map&lt;Object, Object&gt; innerMap;
  
      public CacheWrapper() {
          //initialize the innerMap with an instance for an in-memory cache
          //that works on external server
          //current implementation is not relevant for the problem
          innerMap = ...;
      }
  
      public void putInSharedMemory(Object key, Object value) {
          innerMap.put(key, value);
      }
  
      public Object getFromSharedMemory(Object key) {
          return innerMap.get(key);
      }
  }
  </code></pre>
  
  <p>And my client class (you could say it looks like this):</p>
  
  <pre><code>public class SomeClient {
      //Logger here, for exception handling
      Logger log = ...;
      private CacheWrapper cacheWrapper;
      //getter and setter for cacheWrapper...
  
      public Entity getEntity(String param) {
          Entity someEntity = null;
          try {
              try {
                  entity = cacheWrapper.getFromSharedMemory(param);
              } catch (Exception e) {
                  //probably connection failure occurred here
                  log.warn("There was a problem when getting from in-memory " + param + " key.", e);
              }
              if (entity == null) {
                  entity = ...; //retrieve it from database
                  //store in in-memory cache
                  try {
                      cacheWrapper.put(param, entity);
                  } catch (Exception e) {
                      //probably connection failure occurred here
                      log.warn("There was a problem when putting in in-memory " + param + " key.", e);
                  }
              }
          } catch (Exception e) {
              logger.error(".......", e);
          }
          return entity;
      }
  }
  </code></pre>
  
  <p><strong>I'm creating unit tests for <code>SomeClient#getEntity</code> method</strong> and have to cover all scenarios. For instance, I need to cover the scenario where there are exceptions thrown by <code>cacheWrapper</code>. The approach I'm following is to create a mock for <code>CacheWrapper</code> class, make the methods on <code>CacheWrapper</code> class to throw a <code>RuntimeException</code>, set this mock in an instance of <code>SomeClient</code> and test <code>Someclient#getEntity</code>. The problem is when trying to mock <code>putInSharedMemory</code> method because is <code>void</code>. I have tried lot of ways to do this but none of them work. <strong>The project has dependencies for PowerMock and EasyMock</strong>.</p>
  
  <p>Here are my attempts:</p>
  
  <ol>
  <li>Using <code>EasyMock.&lt;Void&gt;expect</code>. This raised a compiler error.</li>
  <li><p>Tried to stub <code>CacheWrapper#putInSharedMemory</code>. Didn't worked because raised an exception with this error message:</p>
  
  <blockquote>
    <p>java.lang.AssertionError: Unexpected method call putInSharedMemory("foo", com.company.domain.Entity@609fc98)</p>
  </blockquote></li>
  <li><p><strong>Added Mockito dependency to the project</strong> to make use of the functionality of <code>PowerMockito</code> class. But this raised an exception because it doesn't integrate with EasyMock. This is the exception raised:</p>
  
  <blockquote>
    <p>java.lang.ClassCastException: org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl cannot be cast to org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl</p>
  </blockquote></li>
  </ol>
  
  <p>Here's the code for this unit test sample:</p>
  
  <pre><code>@Test
  public void getEntityWithCacheWrapperException() {
      CacheWrapper cacheWrapper = mockThrowsException();
      SomeClient someClient = new SomeClient();
      someClient.setCacheWrapper(cacheWrapper);
      Entity entity = someClient.getEntity();
      //here.....................^
      //cacheWrapper.putInSharedMemory should throw an exception
  
      //start asserting here...
  }
  
  //...
  
  public CacheWrapper mockThrowsException() {
      CacheWrapper cacheWrapper = PowerMock.createMock(CacheWrapper.class);
      //mocking getFromSharedMemory method
      //this works like a charm
      EasyMock.expect(cacheWrapper.getFromSharedMemory(EasyMock.anyObject()))
          .andThrow(new RuntimeException("This is an intentional Exception")).anyTimes();
  
      //mocking putInSharedMemory method
      //the pieces of code here were not executed at the same time
      //instead they were commented and choose one approach after another
  
      //attempt 1: compiler exception: &lt;Void&gt; is not applicable for &lt;void&gt;
      EasyMock.&lt;Void&gt;expect(cacheWrapper.putInSharedMemory(EasyMock.anyObject(), EasyMock.anyObject()))
          .andThrow(new RuntimeException("This is an intentional Exception")).anyTimes();
  
      //attempt 2: stubbing the method
      //exception when executing the test:
       //Unexpected method call putInSharedMemory("foo", com.company.domain.Entity@609fc98)
      Method method = PowerMock.method(CacheWrapper.class, "putInSharedMemory", Object.class, Object.class);
      PowerMock.stub(method).toThrow(new RuntimeException("Exception on purpose."));
  
      //attempt 3: added dependency to Mockito integrated to PowerMock
      //bad idea: the mock created by PowerMock.createMock() belongs to EasyMock, not to Mockito
      //so it breaks when performing the when method
      //exception:
      //java.lang.ClassCastException: org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl
      //cannot be cast to org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl
      //at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:54)
      PowerMockito.doThrow(new RuntimeException("Exception on purpose."))
          .when(cacheWrapper).putInSharedMemory(EasyMock.anyObject(), EasyMock.anyObject());
  
      PowerMock.replay(cacheWrapper);
      return cacheWrapper;
  }
  </code></pre>
  
  <p>I cannot change the implementation of <code>CacheWrapper</code> because it comes from a third party library. Also, I cannot use <code>EasyMock#getLastCall</code> because I'm performing the test on <code>SomeClient#getEntity</code>.</p>
  
  <p>How can I overcome this?</p>