我试图写一个需要request.getHeader('X-File-Size')
来返回Long的内容。所以我试着像这样编写测试:
given:
MockHttpServletRequest request = new MockHttpServletRequest()
request.getHeader('X-File-Size') >> 2L
when:
controller.handle()
then:
1 * serviceMock.upload(_ as Long)
这就是我的控制器的样子:
def handle() {
def fileSize = request.getHeader('X-File-Size') as Long
uploadFileService.upload(fileSize)
}
但是它说我将null传递给服务方法而不是字符串。我认为mock会返回long而不是null。
答案 0 :(得分:0)
使用以下代码:
MockHttpServletRequest request = new MockHttpServletRequest()
您不是创建模拟而是创建真实对象。创建模拟对象您需要使用test的超类中的Mock
或GroovyMock
方法(应为Specification
)。所以:
MockHttpServletRequest request = Mock()//GroovyMock()
request.getHeader('X-File-Size') >> 2L
答案 1 :(得分:0)
测试控制器后,mock request可用时,您试过吗?
setup:
def key = 'X-File-Size'
def value = 2L
request.addHeader(key, value)
when:
controller.handle()
then:
1 * serviceMock.upload(value)