jeudi 23 mars 2017

Mocking exceptions with Mockito: Unexpected Exception error

This is the code of my service:

public class Service {
   public Object serviceMethod() throws MyException {
      Dao dao = new Dao();
      try {
         return dao.dao_method();
      } catch (ExceptionFromDaoMethod e) {
         throw (new MyException());
      }
   }
}

I want to write a simple Unit Test: this is my test case:

@Mock
Dao dao;

@InjectMocks
Service service;

@Test(expected=MyException.class)
public void shouldReturnMyException throws MyException {
   when(service.serviceMethod()).thenThrow(new MyException());
}

This test fails, because I have an Unexpected exception: the expected exception is MyException but was org.mockito.exception.base.MockitoException

Why? What is the solution?

Aucun commentaire:

Enregistrer un commentaire