gpt4 book ai didi

使用 Pact 的 JAVA 契约(Contract)测试(CDC)

转载 作者:行者123 更新时间:2023-11-28 21:21:26 25 4
gpt4 key购买 nike

我正在尝试为此服务编写契约(Contract)测试:

    @RestController
@RequestMapping(path = "/api/form")
public class FormController {

private RestOperations restOperations;

@Autowired
public FormController(RestOperations restOperations) {
this.restOperations = restOperations;
}


@PostMapping(path = "/submit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<SubmitFormResponse> submitForm(@RequestBody @Valid SubmitFormCommand submitFormCommand) {
return restOperations.postForEntity("http://127.0.0.1:9000/api/form/submit", submitFormCommand, SubmitFormResponse.class);
}
}

SubmitFormCommand 仅包含String“消息”,SubmitFormResponse 包含Boolean“成功”

此服务的我的 RestClient:

@Component
public class FormControllerClient {

@Autowired
private RestOperations restOperations;

public ResponseEntity<SubmitFormResponse> submitForm(SubmitFormCommand submitFormCommand) {
HttpEntity<SubmitFormCommand> request = new HttpEntity<>(submitFormCommand);
return restOperations.exchange("http://localhost:1234/api/form/submit", HttpMethod.POST, request, SubmitFormResponse.class);
}

消费者的契约(Contract)测试类如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ContactFormClientTest {

@Rule
public PactProviderRuleMk2 pactProviderRuleMk2 = new PactProviderRuleMk2("formservice", "localhost", 1234, this);
@Autowired
private FormControllerClient formControllerClient;

@Pact(state = "provider accets submit contact form", provider = "formservice", consumer = "formclient")
public RequestResponsePact submitFormPact(PactDslWithProvider builder) {
return builder
.given("provider accetps form submit")
.uponReceiving("a request to POST form")
.path("/api/form/submit")
.method("POST")
.willRespondWith()
.status(200)
.matchHeader("Content-Type", "application/json;charset=UTF-8")
.body(new PactDslJsonBody()
.stringType("message", "TestMessage"))
.toPact();
}

@Test
@PactVerification(fragment = "submitFormPact")
public void verifySubmitFormPact() {
SubmitFormCommand submitFormCommand = new SubmitFormCommand("TestMessage");
ResponseEntity<SubmitFormResponse> response = formControllerClient.submitForm(submitFormCommand);
assertNotNull(response);

}
}

每次我运行测试时它都显示“连接被拒绝”并且我不明白我在设置中做错了什么,在这种情况下我的 FormController 将是一个消费者,因为它调用了另一个服务提交表格。

pom.xml 中用于构建 Pact 文件的插件如下所示:

            <plugin>
<!-- mvn pact:publish -->
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-maven_2.11</artifactId>
<version>3.5.10</version>
<configuration>
<pactDirectory>../pacts</pactDirectory>
<pactBrokerUrl>http://localhost:1234</pactBrokerUrl>
<projectVersion>${project.version}</projectVersion>
</configuration>
</plugin>

最佳答案

问题是您将请求正文放在响应中。你的契约应该是这样的:

@Pact(state = "provider accets submit contact form", provider = "formservice", consumer = "formclient")
public RequestResponsePact submitFormPact(PactDslWithProvider builder) {
return builder
.given("provider accetps form submit")
.uponReceiving("a request to POST form")
.path("/api/form/submit")
.method("POST")
.body(new PactDslJsonBody()
.stringType("message", "TestMessage"))
.willRespondWith()
.status(200)
.matchHeader("Content-Type", "application/json;charset=UTF-8")
.body(new PactDslJsonBody()
.booleanType("sucess", true))
.toPact();
}

关于使用 Pact 的 JAVA 契约(Contract)测试(CDC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com