首页 > 解决方案 > 空手道 0.9.5.RC5 -“重试直到”功能不知何故不尊重我在 karate-config.js 中的“重试”配置

问题描述

我有以下测试:

Feature: News API

  Background:
    * url baseUrl

  Scenario: get news index
    Given path "/"
    And retry until responseStatus == 200
    When method get
    Then status 200
    And match response contains "News service up and running"

和以下 karate-config.js

function fn() {
    // get java system property 'karate.env'
    var env = karate.env;
    karate.log('karate.env system property was:', env);

    var config = {
        baseUrl: 'http://localhost:8080',
    };

    karate.configure('retry', {count: 5, interval: 6000});
    karate.configure('connectTimeout', 5000);
    karate.configure('readTimeout', 5000);

    return config;
}

使用 maven-failsafe-plugin 执行测试后,控制台中出现以下错误:

...
[INFO] Running newsservice.NewsServiceIT
[main] INFO  o.a.http.impl.execchain.RetryExec - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
[main] INFO  o.a.http.impl.execchain.RetryExec - Retrying request to {}->http://localhost:8080
[main] INFO  o.a.http.impl.execchain.RetryExec - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
[main] INFO  o.a.http.impl.execchain.RetryExec - Retrying request to {}->http://localhost:8080
[main] INFO  o.a.http.impl.execchain.RetryExec - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
[main] INFO  o.a.http.impl.execchain.RetryExec - Retrying request to {}->http://localhost:8080
[main] ERROR com.intuit.karate - org.apache.http.NoHttpResponseException: localhost:8080 failed to respond, http call failed after 2 milliseconds for URL: http://localhost:8080/
[main] ERROR com.intuit.karate - http request failed: 
org.apache.http.NoHttpResponseException: localhost:8080 failed to respond
---------------------------------------------------------
feature: classpath:newsservice/news/news-index.feature
scenarios:  1 | passed:  0 | failed:  1 | time: 0.7678
---------------------------------------------------------

HTML report: (paste into browser to view) | Karate version: 0.9.5.RC5
...

在测试开始之前,我启动了我的 Spring Boot 应用程序,这需要一些时间(约 5-7 秒),我知道测试没有成功,因为 Sprint Boot 应用程序尚未启动。

这就是为什么我尝试使用retry until空手道的这个功能来确保它在一段时间后重试。

retry但根据控制台输出,似乎不尊重配置。似乎它总是只尝试3次......

我还尝试retry在测试文件本身中设置配置,就像在空手道文档中一样:

* configure retry = { count: 10, interval: 5000 }

但这也没有用。

也许你有一个提示为什么它不起作用或者我仍然错过了什么?

谢谢你的支持!

标签: javaapispring-boottestingkarate

解决方案


retry until只有建立 HTTP 连接后,问题才会出现。您需要想办法等到您的服务器可以接受连接。

为什么您看到 3 次尝试是因为这是底层 Apache HTTP 客户端的默认行为。

您应该能够编写或重用一些实用程序来执行此操作。看一下 Karate internals 中的这段代码,寻找waitForHttp方法:

https://github.com/intuit/karate/blob/develop/karate-core/src/main/java/com/intuit/karate/shell/Command.java#L112

在我看来,执行此“等待”的最佳位置是在启动空手道套件的 JUnit / Java 代码中 - 您可能已经拥有用于启动 Spring Boot 的套件。


推荐阅读