1

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>
Harsha pps
2,2584 gold badges29 silver badges38 bronze badges
asked Jun 21, 2018 at 16:15

1 Answer 1

1

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();
 }
answered Jun 22, 2018 at 0:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.