我正在尝试为此服务编写合同测试:
@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 “ message”,而 SubmitFormResponse 包含 Boolean “ success”
此服务的我的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);
}
消费者的合同测试类如下:
@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>
答案 0 :(得分:1)
问题是您将请求正文放在响应中。您的协定应如下所示:
@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();
}