title: Mocking exceptions with Mockito: Unexpected Exception error
body: <p>This is the code of my service:</p>
  
  <pre><code>public class Service {
     public Object serviceMethod() throws MyException {
        @Autowired
        Dao dao;
  
        try {
           return dao.dao_method();
        } catch (ExceptionFromDaoMethod e) {
           throw (new MyException());
        }
     }
  }
  </code></pre>
  
  <p>I want to write a simple Unit Test; this is my test case:</p>
  
  <pre><code>@Mock
  Dao dao;
  
  @InjectMocks
  Service service;
  
  @Test(expected=MyException.class)
  public void shouldReturnMyException throws MyException {
     when(service.serviceMethod()).thenThrow(new MyException());
  }
  </code></pre>
  
  <p>This test fails, because I have an Unexpected exception: </p>
  
  <blockquote>
    <p>the expected exception is MyException but was
    org.mockito.exception.base.MockitoException</p>
  </blockquote>
  
  <p>Why? What is the solution?</p>
  
  <p><strong>Update</strong>
  Thanks to @Maciej Kowalski I have noted that I was using the <em>when</em> condition versus a real class and not versus a mocked one. 
  So I added the annotation <code>@Spy</code> to the Service in the Unit Test.<br> The code of the new test is:</p>
  
  <pre><code>@Mock
  Dao dao;
  
  @InjectMocks
  @Spy
  Service service;
  
  @Before
  MockitoAnnotations.initMocks(this);
  
  @Test(expected=MyException.class)
  public void shouldReturnMyException throws MyException {
     doThrow(new MyException()).when(service.serviceMethod());
  }
  </code></pre>
  
  <p>But now I have this problem:</p>
  
  <blockquote>
    <p>the expected exception is MyException but was
    </p>
  </blockquote>