Pact/JUnit5测试搭建失败:报错未找到node_server对应的@Pact方法
问题根因
@PactTestFor的pactMethod参数匹配规则错误
Pact JUnit5扩展识别@Pact标注方法的规则是匹配Java类中的实际方法名,并非匹配DSL中uponReceiving传入的交互描述文本。你当前在测试方法上配置pactMethod = "notes",但被@Pact注解标记的方法实际名为createPact,"notes"只是交互描述字符串,和方法名无对应关系。扩展扫描不到名为"notes"的@Pact方法,就会抛出“找不到对应provider的@Pact方法”的异常。- 在
@BeforeEach生命周期方法中错误注入MockServer参数
Pact扩展的MockServer参数解析仅覆盖被@Test标注、且正确匹配@Pact方法的测试方法,生命周期钩子方法(@BeforeEach/@AfterEach等)不在解析范围内,直接在这类方法上声明MockServer参数会触发参数解析失败。 - 测试方法中的响应断言逻辑存在实现错误
你直接对httpResponse.getEntity().getContent()返回的InputStream对象调用toString(),得到的是流对象的内存地址哈希值,并非响应体的实际文本内容,就算前置配置问题全部修复,这里也会出现断言不匹配。
修正步骤
- 将测试方法上的
@PactTestFor注解中的pactMethod值修改为实际的@Pact方法名createPact,替换原来错误的"notes"值 - 移除
@BeforeEach方法上的MockServer参数,将MockServer非空校验逻辑挪到测试方法内部执行 - 调整响应体读取逻辑,将InputStream转换为真实字符串内容后再做断言比对
可正常运行的修正后代码
package com.example.demo.integration.pact; import au.com.dius.pact.consumer.MockServer; import au.com.dius.pact.consumer.dsl.PactDslWithProvider; import au.com.dius.pact.consumer.junit5.PactConsumerTestExt; import au.com.dius.pact.consumer.junit5.PactTestFor; import au.com.dius.pact.core.model.RequestResponsePact; import au.com.dius.pact.core.model.annotations.Pact; import org.apache.http.HttpResponse; import org.apache.http.client.fluent.Request; import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; @ExtendWith(PactConsumerTestExt.class) @PactTestFor(providerName = PactTest.PACT_PROVIDER_NAME) public class PactTest { public static final String PACT_PROVIDER_NAME = "node_server"; public static final String PACT_CONSUMER_NAME = "spring_application"; @Pact(provider = PACT_PROVIDER_NAME, consumer = PACT_CONSUMER_NAME) public RequestResponsePact createPact(PactDslWithProvider builder) { return builder .uponReceiving("notes") .path("/notes") .method("GET") .willRespondWith() .matchHeader("Content-Type","application/json") .status(200) .body(getJsonArrayOfNotes(2).toString()) .toPact(); } @Test @PactTestFor(pactMethod = "createPact") void test(MockServer mockServer) throws IOException { Assertions.assertNotNull(mockServer); HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/notes").execute().returnResponse(); assertEquals(200, httpResponse.getStatusLine().getStatusCode()); String responseBody = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())) .lines() .collect(Collectors.joining()); assertEquals(getJsonArrayOfNotes(2).toString(), responseBody); } private JSONArray getJsonArrayOfNotes(int size) { var responseJsonObject = new JSONArray(); for (int i = 0; i < size; i++) { var note = new JSONObject(); try { note.put("title", String.format("Title %s", i + 1)); note.put("content", String.format("Some Note Content of Note %s", i + 1)); } catch (Exception ignored) { } responseJsonObject.put(note); } return responseJsonObject; } }
内容的提问来源于stack exchange,提问作者Kerem
相关产品推荐
相关产品推荐

