I'm having a problem with Spock specifications and exception handling.
I have some code that calls a service and catches a certain type of exception. Within the catch block another type of exception is thrown:
try {
int result = service.invoke(x,y);
...
} catch (IOException e) {
throw BusinessException(e);
}
The following test case works using mockito mocks:
given: "a service that fails"
def service = mock(Service)
when(service.invoke(any(),any())).thenThrow(new IOException())
and: "the class under test using that service"
def classUnderTest = createClassUnderTest(service)
when: "the class under test does something"
classUnderTest.doSomething()
then: "a business exception is thrown"
thrown(BusinessException)
All Tests Passed
but the following test case fails when using Spock to handle the interactions of the service:
given: "a service that fails"
def service = Mock(Service)
service.invoke(_,_) >> { throw new IOException() }
and: "the class under test using that service"
def classUnderTest = createClassUnderTest(service)
when: "the class under test does something"
classUnderTest.doSomething()
then: "a business exception is thrown"
thrown(BusinessException)
Expected exception of type BusinessException', but no exception was thrown
I'm not sure what is going on here. It works for mockito but not for Spock.
I'm not verifying the service that throws the exception, so it does not need to be setup in the when/then block.
(using groovy-all:2.4.5, spock-core:1.0-groovy-2.4)
Aucun commentaire:
Enregistrer un commentaire