【问题标题】:how to write RestTemplate.put() in groovy using Spock如何使用 Spock 在 groovy 中编写 RestTemplate.put()
【发布时间】:2015-08-31 06:10:36
【问题描述】:

我正在使用 groovy 中的 spock 编写集成测试用例。我们正在使用 restTemplate 来调用操作,例如这样

when:
String url="http://localhost:$port/user/password"
ResponseEntity<ResponseWrapper<User>> entity=restTemplate.postForEntity(url,changePasswordDTO,ResponseWrapper.class)

then:
    entity.statusCode == HttpStatus.OK
    entity!=null

我已经为 POST 和 GET 编写了集成测试,但是现在当我为 PUT 编写它时,我在 entity object 上遇到空指针异常

我已经为 PUT 写了这个,我知道 put 操作将是空的,但是我没有任何其他方法,有人可以帮助我

String url="http://localhost:$port/applicant/{id}/status?blacklistingFlag&reason"
    map.put("id", "23")
    map.put("blacklistingFlag","0")
    map.put("reason","no reason")
    restTemplate.exchange(url, HttpMethod.PUT, null, null, map)
    ResponseEntity<ResponseWrapper<User>> entity=restTemplate.put(url,ResponseWrapper.class , map)

    then:
    entity.statusCode == HttpStatus.OK
    entity!=null

【问题讨论】:

    标签: groovy integration-testing spock


    【解决方案1】:

    我看到这是一篇旧帖子,但不能不回答就离开。 restTemplate.put 是一个 void 方法,因此您将无法从中获取回调。

    你应该使用restTemplate.exchange,因为你会得到一个ResponseEntity对象作为回报。

    检查this post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2013-12-29
      相关资源
      最近更新 更多