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);
}
}
Everytime i run the test it says "Connection refused" and i don't understand what i did wrong with a setup, my FormController would be consumer in this case since it calls other 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>
Aucun commentaire:
Enregistrer un commentaire