title: mockito simulate test exception thrown by service
body: <p>I am trying to test exception thrown from service when I use the instance of it.
  Like I am trying to use Imock in Trans.class and use IMock method.
  Below is the code</p>
  <pre><code>public class MockImpl implements IMock{
      public String external(String str) throws Exception {
          if(str.equals(&quot;throw&quot;)){
              throw new Exception(&quot;Thrown exception.&quot;);
          }
          return str;
      }
  }
  
  public class Trans {
      private IMock mc;
      public static int failed;
  
      public String performTrans(String str) throws Exception {
  
          return call(str);
  
      }
  
      private String call(String str) throws Exception {
          mc = new MockImpl();
          try {
              return mc.external(str);
          }
          catch(Exception e){
              failed++;
              throw e;
          }
      }
  }
  </code></pre>
  <p>In test class I am trying to do this</p>
  <pre><code>public class TestMock {
  
      @Test
      public void testMock() throws Exception {
          Trans trans = mock(Trans.class);
          IMock iMock = mock(IMock.class);
  
          doThrow(new Exception()).when(iMock).external(any(String.class));
  
          for(int i =0;i&lt;10 ;i++){
              trans.performTrans(&quot;any&quot;);
          }
          System.out.println(Trans.failed);
          assertEquals(9, Trans.failed);
      }
  
  }
  </code></pre>
  <p>As I am new to this, I am not sure if my understanding is correct, what I am trying to achieve is
  When I do # Trans.performTrans(String);
  Then doThrow(new Exception()).when(iMock).external(any(String.class)); should happen. <strong>How can I tell Mockito or any test framework that The exception should be thrown should be simulated from the Imock service method, even if it is indirectly called.</strong></p>
  <p><strong>UPDATE</strong></p>
  <p>After trying to test this way
  @RunWith(MockitoJUnitRunner.class)
  public class TestMock {</p>
  <pre><code>@Test
  public void testMock() throws Exception {
     Trans trans = new Trans();
     IMock iMock = mock(IMock.class);
  
     trans.setInter(iMock);
  
      //doThrow(new Exception()).when(iMock).external(any(String.class));
  
      trans.performTrans(&quot;abc&quot;);
      verify(iMock).external(new String(&quot;a&quot;));
  
      System.out.println(Trans.failed);
      assertEquals(9, Trans.failed);
  }
  </code></pre>
  <p>}</p>
  <p>I get this error.</p>
  <blockquote>
  <p>Wanted but not invoked: iMock.external(&quot;a&quot;);
  -&gt; at com.app.TestMock.testMock(TestMock.java:32) Actually, there were zero interactions with this mock.</p>
  </blockquote>
  <p>what could be wrong?</p>