vendredi 27 novembre 2015

how to mock in jMock a DataAccessException?

I am working over some code that needs some tests.

Nothing difficult, but there are couple of methods that throw a DataAccessException in this way

public void someMethod(String someString) throws DataAccessException{
   myService.doSomething
}

Now on my test I want to be sure (after sending a parameter I know it will make my service throw the exception) that I can mock it , call the doSomething method and it will return the DataAccessException Exception, but I cannot do it since DataAccessException cannot be instantiated.

Any idea how to do it?

So far I just have this

@Test
public void testSomeMethod() throws DataAccessException {

    mockery.checking(new Expectations() {
        {
            oneOf(service).doSomething(someString);
            // HERE I WANT TO BE SURE I GET AN EXCEPTION
            will(throwException(new DataAccessException("") {}));
        }
    });
    service.doSomething(null);
}

So far I just get an error with no descriptions more than at org.jmock.lib.action.ThrowAction.invoke

And I cannot change to mockito or any other mocking/testing framework.

Aucun commentaire:

Enregistrer un commentaire