【发布时间】:2015-06-15 22:39:32
【问题描述】:
我有一个使用另一个服务的服务,例如:
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)
}
}
}
现在我正在尝试编写一个单元测试,通过使用以下规范模拟该函数来检查异常是从bServ.functionB() 引发并由functionA() 处理的。
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")
}
}
但是我得到一个错误,这个测试说 log.warn 语句(0 次调用)。
希望了解有关此测试为何不正确以及我如何测试functionA() 正确处理异常的任何见解
【问题讨论】:
-
functionA()需要服务类中的 Map 参数。测试没有使用 Map 作为参数调用functionA()。 -
如果您觉得我的回答有用,请接受并点赞。