【发布时间】:2016-02-16 20:20:37
【问题描述】:
谁能告诉我这是一个错误还是预期的行为。
我知道在 Spock 中我可以测试私有方法:
def "test with private"() {
given:
FileContentValidator fileContentValidator = new FileContentValidator(1)
when:
fileContentValidator.validateCustomerSiteId("") // this is a private method
then:
true // succeeds
}
但是当我使用 Spock Spy 尝试同样的事情时,它失败了:
def "test with private on spy"() {
given:
FileContentValidator fileContentValidator = Spy(FileContentValidator, constructorArgs: [1])
when:
fileContentValidator.validateCustomerSiteId("") // this is a private method
then:
true // does not get here
}
我得到一个例外:
groovy.lang.MissingMethodException: No signature of method: com.shoppertrak.device.management.web.validator.ophour.FileContentValidator$$EnhancerByCGLIB$$7ff6a42.validateCustomerSiteId() is applicable for argument types: (java.lang.String) values: []
【问题讨论】:
-
似乎轻率地将其称为错误。间谍的工作是窥探合约中的方法,这是一个灵活的概念,但很难找到包含声明为私有的东西的定义。
-
那么也许将其归入“预期行为”类别
-
FileContentValidator 是类类型还是接口类型?间谍总是基于真实对象,您必须提供类类型而不是接口类型。
-
FileContentValidator是一个类 -
尝试将方法更改为受保护。