如何在Spock groovy中模拟RequestEntity.put()和restTemplate.exchange()

问题描述 投票:0回答:1

用于api调用的Java代码

我想知道如何测试以下两行代码。

private void api(){
    //Code to call an API and i want to test this in groovy spock
    HttpHeaders httpHeaders = new HttpHeaders();      
    httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON); 
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    RestTemplate restTemplate = new RestTemplate();
    String url ="url";
    String body ="body";
    //How to mock below line
    RequestEntity<String> requestEntity = RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
    //And this line
    ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity,String.class);
    HttpStatus StatusCode = responseEntity.getStatusCode();
}
java spring testing groovy spock
1个回答
1
投票

这不是这样的Spock问题(您没有展示Spock规范的任何一行,因此到目前为止没人知道您已经尝试过什么),而是软件工程或常规测试问题。测试意大利面条代码的问题-在这里我的意思是既执行某项操作又同时创建许多对象的代码-从外部无法访问在方法内部创建并存储在局部变量中的对象。有两种方法可以重构代码以提高可测试性:

  • 如果有意义,请更改代码,以使用户能够从外部注入依赖关系,而不是在内部创建依赖关系的代码。请谷歌“依赖注入”,并找到类似的变体

    • 构造函数注入,
    • 注浆剂,
    • 方法参数注入,
    • 使用CDI之类的工具/范例时的场注入。
  • 另一种方法是将方法的对象创建部分分解为较小的生产者(或创建者或工厂)方法,然后在使用部分模拟(间谍)时根据您在测试中的选择可以覆盖(存根) )对象。 Spock提供了这类间谍,因此您可以轻松使用它们。

我将显示后一种方法供您参考:

package de.scrum_master.stackoverflow.q58101434;

import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

public class MyRestApi {
  public HttpStatus api() throws URISyntaxException {
    //Code to call an API and i want to test this in groovy spock
    HttpHeaders httpHeaders = createHttpHeaders();
    RestTemplate restTemplate = createRestTemplate();
    String url ="url";
    String body ="body";
    //How to mock below line
    RequestEntity<String> requestEntity = createRequestEntity(url, body);
    //And this line
    ResponseEntity<String> responseEntity = executeRequest(restTemplate, requestEntity);
    return responseEntity.getStatusCode();
  }

  HttpHeaders createHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    return httpHeaders;
  }

  RestTemplate createRestTemplate() {
    return new RestTemplate();
  }

  RequestEntity<String> createRequestEntity(String url, String body) throws URISyntaxException {
    return RequestEntity.put(new URI(url)).contentType(MediaType.APPLICATION_JSON).body(body);
  }

  ResponseEntity<String> executeRequest(RestTemplate restTemplate, RequestEntity<String> requestEntity) {
    return restTemplate.exchange(requestEntity,String.class);
  }
}

现在看看该方法的结构和可读性如何(仍可以改进,我只是以一种快速而肮脏的方式做到了?)?请特别注意辅助方法createRequestEntityexecuteRequest

现在这是您如何编写Spock测试的方法:

package de.scrum_master.stackoverflow.q58101434

import org.springframework.http.HttpStatus
import org.springframework.http.RequestEntity
import org.springframework.http.ResponseEntity
import spock.lang.Specification
import spock.lang.Unroll

class MyRestApiTest extends Specification {
  @Unroll
  def "API returns status code #statusCode"() {
    given: "prepare mocks + spy"
    RequestEntity<String> requestEntity = Mock()
    ResponseEntity<String> responseEntity = Mock() {
      getStatusCode() >> httpStatus
    }
    MyRestApi myRestApi = Spy() {
      createRequestEntity(_, _) >> requestEntity
      executeRequest(_, _) >> responseEntity
    }

    when: "execute API method"
    def result = myRestApi.api()

    then: "check expected results"
    // This actually only tests mockfunctionality, your real test would look differently
    statusCode == result.value()
    reasonPhrase == result.reasonPhrase

    where:
    httpStatus                   | statusCode | reasonPhrase
    HttpStatus.OK                | 200        | "OK"
    HttpStatus.MOVED_PERMANENTLY | 301        | "Moved Permanently"
    HttpStatus.UNAUTHORIZED      | 401        | "Unauthorized"
  }
}

如果您不了解此代码,请随时提出(相关!)后续问题。我建议您学习更多有关干净代码,可测试性,一般的模拟测试,尤其是有关Spock的信息。

© www.soinside.com 2019 - 2024. All rights reserved.