简体   繁体   中英

JAVA Contract testing (CDC) using Pact

I'm trying to write contract test to this service:

    @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 contains only String "message" and SubmitFormResponse contains Boolean "success"

My RestClient for this service:

@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);
    }

And Contract test class of consumer looks like this:

@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);

    }
}

Every time I run the test it says "Connection refused" and I don't understand what I did wrong with a setup, my FormController would be a consumer in this case since it calls another service to submit the form.

Plugin in pom.xml for building Pact file looks like this :

            <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>

The problem is you are placing your request body in the response. Your pact should look like:

@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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM