【问题标题】:WireMock On Spring Boot Test With External Https Host使用外部 Https 主机进行 Spring Boot 测试的 WireMock
【发布时间】:2020-09-18 15:57:40
【问题描述】:

在我的应用程序中,我有外部第三方 API 请求。我想通过模拟这个 API 请求来测试我的应用程序。

我的服务等级:

String API_URL = "https://external.com/v1/%s";
public Result executeRequest(String apiVersion, String subUrl, HttpMethod httpMethod)
{
    try
    {
        HttpRequestBase httpRequest;
        String url = String.format(API_URL, subUrl);
        if (httpMethod.equals(HttpMethod.GET))
        {
            httpRequest = new HttpGet(url);
        }
        else if (httpMethod.equals(HttpMethod.POST))
        {
            httpRequest = new HttpPost(url);
            ((HttpPost) httpRequest).setEntity(new StringEntity(requestBody, "UTF-8"));
        }
        ...
        headers.forEach(httpRequest::setHeader);
        HttpResponse response = httpClient.execute(httpRequest);
    }
    catch (IOException e)
    {
        logger.error("IO Error: {}", e.getMessage());
        return handleExceptions(e);
    }
}

总结服务类,请求可以是get、post、delete、put。并且此请求将使用标头或正文部分进行处理。然后将作为http请求处理。

我的测试课:

@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class ServiceTest
{

    private static final String API_URL = "https://external.com/v1";

    @Autowired
    private Service service;

    @Autowired
    protected Gson gson;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule();

    @Test
    public void getResult_successfully()
    {
        Result result = new Result();
        wireMockRule.stubFor(get(urlPathMatching("/subUrl"))
                .willReturn(aResponse()
                        .proxiedFrom(API_URL)
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json")
                        .withBody(gson.toJson(result))));

        Result returnResult = service.executeRequest("/subUrl", GET);

        assertThat(returnResult).isEqualTo(result);
    }
}

当我像上面那样实现它时,模拟不起作用。有什么建议吗?

注意:我希望代码 sn-ps 将足以理解整个代码。

【问题讨论】:

    标签: java spring-boot mocking spring-boot-test wiremock


    【解决方案1】:

    我解决了;

    1. 描述测试 api-url 和端口。因为 Wiremock 创建了嵌入式 localhost 服务器。

    所以在应用程序属性中:

    application-test.properties: 
      - service.url: http://localhost:8484
    application-prod.properties: 
      - service.url: https://external.com/v1
    

    然后是我的测试类:

    @ClassRule
    public static WireMockRule wireMockRule = new WireMockRule(8484);
    
    @Test
    public void getResult_successfully()
    {
        Result result = new Result();
        wireMockRule.stubFor(get(urlPathMatching("/subUrl"))
                .willReturn(aResponse()
                        .proxiedFrom(API_URL)
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json")
                        .withBody(gson.toJson(result))));
    
        Result returnResult = service.executeRequest("/subUrl", GET);
    
        assertThat(returnResult).isEqualTo(result);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2021-09-12
      • 2016-07-01
      • 2019-04-01
      • 2018-02-01
      • 2019-03-12
      相关资源
      最近更新 更多