【发布时间】:2015-10-07 01:20:34
【问题描述】:
我一直在使用 spock 对我的 java 项目进行单元测试,但遇到了问题。我有一个实用方法可以从 http 请求中获取参数,或者如果 http 请求为空,则为空字符串,并且我正在尝试使用 spock 对其进行测试。我的测试如下所示:
package foo.bar.test
import foo.bah.HttpRequestPropertyLoader
import spock.lang.Unroll
import javax.servlet.http.HttpServletRequest
import spock.lang.Specification
class HttpRequestPropertyLoaderTest extends Specification {
HttpRequestPropertyLoader subjectUnderTest
def result
def setup() {
subjectUnderTest = new HttpRequestPropertyLoader()
}
@Unroll("When my http request is #nullOrNot then when I get parameter from it the response=#response" )
def "Test load data from request"() {
given:
HttpServletRequest mockHttpRequest = Mock()
mockHttpRequest.getAttribute("foo") >> "bar"
when:
result = subjectUnderTest.loadStringFromHttpRequest(httpRequest, "foo")
then:
result == response
where:
httpRequest | response | nullOrNot
null | "" | "null"
mockHttpRequest | "bar" | "not null"
}
}
但是,当我运行此测试时,我收到以下错误:
groovy.lang.MissingPropertyException: No such property: mockHttpRequest for class: foo.bar.test.HttpRequestPropertyLoaderTest at foo.bar.test.HttpRequestPropertyLoaderTest.Test load data from request(HttpRequestPropertyLoaderTest.groovy)
经过研究,我了解到where 块在given 块之前运行,因此出现错误,但只是想知道是否有解决方法?
我知道要使用测试外部的变量,我需要使用 @Shared 注释来注释变量,这对我来说似乎是不好的做法。每个测试都应该与其他测试完全分开运行,所以不要真的希望有一个对象在测试之间保持其状态。
是否可以设置 Mock 对象以其他方式从 where 块返回?
【问题讨论】:
-
您是否尝试在测试开始时将
HttpServletRequest mockHttpRequest = Mock()移动到setup块? -
@tim_yates 感谢您的建议。如果这就是您的意思,我只是尝试用设置块替换给定块,并得到相同的结果。
-
四处搜索,我认为您需要将模拟类移出
@Shared类级别字段 -
@tim_yates 是的,我之前看过
@Shared注释,但正如其中一个答案所说,'缺点是 a 和 o 在某种意义上定义在错误的范围内,可以由其他特征方法也是如此。正如我在编辑中所说,这对我来说似乎是一种不好的做法,但如果没有其他选择,我将不得不研究它。
标签: unit-testing groovy spock