【发布时间】:2012-03-01 20:57:43
【问题描述】:
在 Grails 的控制器单元测试(更具体地说是 Spock ControllerSpec)中,我想检查当协作者抛出异常时测试方法的行为。
我正在使用 mockFor 实用程序(来自 Spock 的 UnitSpec 或 Grails 的 GrailsUnitTestMixin)来指定我对测试中这种异常抛出方法的要求,如下所示:
@TestFor(TestController)
class TestControllerSpec extends Specification {
def "throwing and exception from a mock method should make the test fail"() {
setup:
def serviceMock = mockFor(TestService)
serviceMock.demand.exceptionThrowingMethod() { throw new Exception() }
controller.testService = serviceMock.createMock()
when:
controller.triggerException()
then:
thrown(Exception)
}
}
所以,在triggerException 内部,我调用exceptionThrowingMethod,如下所示:
class TestController {
def testService
def triggerException() {
testService.exceptionThrowingMethod()
}
}
但是测试失败了:
预期的异常 java.lang.Exception,但没有抛出异常
我调试了执行并且没有抛出异常,exceptionThrowingMethod 的调用令人惊讶地返回了一个闭包。没关系将throws 声明添加到方法的签名中,也不起作用。
我认为这与 Spock 有关,但我尝试了一个类似的测试,只使用 grails 的测试混合并得到了相同的结果。这是我的尝试:
@TestFor(TestController)
class TestControllerTests {
void testException() {
def serviceMock = mockFor(TestService)
serviceMock.demand.exceptionThrowingMethod() { throw new Exception() }
controller.testService = serviceMock.createMock()
shouldFail(Exception) {
controller.triggerException()
}
}
}
你发现我的代码有什么问题吗?
我在 Grails 的文档中的任何地方都找不到如何要求抛出异常,所以上面的代码对我来说听起来很自然。
我还发现通过谷歌搜索没有找到任何相关的东西很可疑,所以也许我在测试方面做错了事情。
这不是测试中的常见情况吗?您在特定场景中模拟某些方法的确定性行为,然后在这种场景发生时测试被测方法的预期行为。对我来说,抛出异常似乎是一个有效的场景。
【问题讨论】:
标签: unit-testing testing grails mocking