我正在为grails中的API编写集成测试。此测试中的API接受JSON请求并仅返回JSON响应。让我们说请求JSON是: {" param1":value1," param2":value2}
在此请求中,value1和value2取决于其他一些变量。那么我怎样才能将它们放入请求JSON
中代码段:
void testMethod(){
def controller = new DummyController()
Long param1Value = getValue(10)
Long param2Value = getValue(20)
controller.request.content = '{"param1":<param1Value>,"param2":<param2Value>}'
controller.methodUnderTest()
def response = controller.response.json
}
在此,我如何分别为param1和param2参数指定param1value和param2value。请帮忙!
答案 0 :(得分:0)
您可以使用JsonBuilder
,如下所示。
void testMethod(){
def controller = new DummyController()
def reqJson = new groovy.json.JsonBuilder()
reqJson {
params1 getValue(10)
params2 getValue(20)
}
controller.request.content = reqJson.toString()
controller.methodUnderTest()
def response = controller.response.json
}
或使用"""
或斜杠字符串作为json字符串:
controller.request.content = """{"param1":"$param1Value","param2":"$param2Value"}"""
controller.request.content = /{"param1":"$param1Value","param2":"$param2Value"}/