【问题标题】:Pact Provider @State Test always returning 404Pact Provider @State Test 总是返回 404
【发布时间】:2017-09-23 11:21:20
【问题描述】:

我能够为 Spring Boot 项目运行测试,但我在 @State 测试中总是得到 404。

@TargetRequestFilter
public void exampleRequestFilter(HttpRequest request) {
  System.out.println(request.toString());
  request.addHeader("Authorization", JIMMY_CARTER_TOKEN);
}

@BeforeClass
public static void setupApplication() {
  SpringApplication application = new SpringApplication(App.class);
  application.setAdditionalProfiles("integration");
  application.run("--server.port=9000");
}

@TestTarget
public final HttpTarget target = new HttpTarget("http", "127.0.0.1", 9000);

@State("user id") // Method will be run before testing interactions that require "default" or "no-data" state
public void toUserId() {
    System.out.println("Test User Id");
}

奇怪的是,我可以通过打印出请求信息和 Authorization 标头来判断它到达了正确的端点。我将调试语句放入并验证我可以使用与测试相同的凭据和端点进行调用。但是,测试总是以 404 失败。我的设置中是否缺少某些内容?

"request": {
            "method": "GET",
            "path": "/api/user/XXXXXX"
        },
        "response": {
            "status": 200,
            "headers": {
                "content-type": "application/vnd.api+json;charset=UTF-8"
            },
            "body": ... 
},
        "providerStates": [
            {
                "name": "user id"
            }
        ]
    }

【问题讨论】:

    标签: spring spring-boot pact pact-jvm


    【解决方案1】:

    您可以通过使用 Apache HTTP 客户端和 pact-jvm 库启用调试日志记录来查看正在发出的请求。 Apache HTTP Client,请参考https://hc.apache.org/httpcomponents-client-ga/logging.html。

    有关您要查找的调试日志的示例,这是来自 pact-jvm (https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-provider-junit/src/test/java/au/com/dius/pact/provider/junit/ContractTest.java) 的示例 ContractTest:

        13:09:20.012 [Test worker] DEBUG au.com.dius.pact.provider.ProviderClient - Making request for provider au.com.dius.pact.provider.ProviderInfo(http, localhost, 8332, /, myAwesomeService, null, null, au.com.dius.pact.provider.junit.target.HttpTarget$$Lambda$14/771479970@1dec1536, null, null, false, null, changeit, null, true, false, true, null, [], []):
        13:09:20.018 [Test worker] DEBUG au.com.dius.pact.provider.ProviderClient -     method: GET
              path: /data
              query: [:]
              headers: [:]
              matchers: MatchingRules(rules=[:])
              generators: Generators(categories={})
              body: OptionalBody(state=MISSING, value=null)
            13:09:20.475 [Test worker] INFO au.com.dius.pact.provider.junit.ContractTest - exampleRequestFilter called: GET http://localhost:8332/data HTTP/1.1
        13:09:20.537 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> GET /data HTTP/1.1
        13:09:20.538 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8332
        13:09:20.538 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
        13:09:20.551 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_131)
        13:09:20.553 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
        13:09:20.553 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "GET /data HTTP/1.1[\r][\n]"
        13:09:20.554 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: localhost:8332[\r][\n]"
        13:09:20.555 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
        13:09:20.558 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_131)[\r][\n]"
        13:09:20.559 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
        13:09:20.560 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
        13:09:20.774 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 204 No Content[\r][\n]"
        13:09:20.775 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Sat, 23 Sep 2017 03:09:20 GMT[\r][\n]"
        13:09:20.775 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: rest-client-driver(1.1.45)[\r][\n]"
        13:09:20.779 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
        13:09:20.784 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 204 No Content
        13:09:20.785 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Sat, 23 Sep 2017 03:09:20 GMT
        13:09:20.785 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Server: rest-client-driver(1.1.45)
        13:09:20.842 [Test worker] DEBUG au.com.dius.pact.provider.ProviderClient - Received response: HTTP/1.1 204 No Content
        13:09:20.867 [Test worker] DEBUG au.com.dius.pact.provider.ProviderClient - Response: [statusCode:204, headers:[Date:Sat, 23 Sep 2017 03:09:20 GMT, Server:rest-client-driver(1.1.45)]]
        13:09:21.724 [Test worker] DEBUG au.com.dius.pact.model.Matching$ - Found a matcher for text/plain -> Some((text/plain,au.com.dius.pact.matchers.PlainTextBodyMatcher@29c3e77b))
            returns a response which
              has status code 204 (OK)
              has a matching body (OK)
    

    【讨论】:

    • 我还发现这有助于 Spring Boot 捕获所有传入和传出请求。 mdeinum.wordpress.com/2015/07/01/spring-framework-hidden-gems 我仍在努力找出为什么我得到 404 并试图弄清楚为什么内部测试失败了为什么外部调用工作正常。
    • @JoelHolmes 你有没有发现它返回 404 的原因?
    • 好吧,即使是指向 ContractTest.java 源的链接现在也返回 404
    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 2015-07-31
    • 1970-01-01
    • 2016-02-04
    • 2014-05-29
    • 2021-07-28
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多