I have a service which uses another service, something like:
class ServiceA implements IFaceClass {
ServiceB bServ
@Override
void functionA(Map map) {
try {
bServ.functionB() // this throws an exception on error
} catch (Exception e) {
log.warn("Exception occured: " + e.message)
}
}
}
Now I am trying to write a unit test that checks that the Exception is raised from bServ.functionB()
and handled by functionA()
by mocking that function with the following Spec.
class ServiceASpec extends Specification {
ServiceB bServ
def setup() {
bServ = Mock(ServiceB)
service.bServ = bServ
}
def "test exception raised by functionB is handled"() {
given:
bServ.functionB() >> {
throw new Exception("some exception message")
}
when:
service.functionA()
then:
1 * log.warn("Exception occured: some exception message")
}
}
However I get an error with this test saying (0 invocations) of the log.warn statement.
Would appreciate any insights into why this test is not correct and how I can test that the exception is being handled correctly by functionA()
Here You can find sample working test: