JAVA-4493: Add missing live test for Consumer Driven Contracts article (#12362)
* JAVA-4493: Add missing live test for Consumer Driven Contracts article * JAVA-4493: Add missing live test for Consumer Driven Contracts article
This commit is contained in:
-76
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.pact;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import au.com.dius.pact.consumer.MockServer;
|
||||
import au.com.dius.pact.consumer.Pact;
|
||||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
|
||||
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
|
||||
import au.com.dius.pact.consumer.junit5.PactTestFor;
|
||||
import au.com.dius.pact.model.RequestResponsePact;
|
||||
|
||||
@ExtendWith(PactConsumerTestExt.class)
|
||||
@PactTestFor(providerName = "test_provider", hostInterface="localhost")
|
||||
public class PactConsumerDrivenContractUnitTest {
|
||||
|
||||
@Pact(provider="test_provider", consumer = "test_consumer")
|
||||
public RequestResponsePact createPact(PactDslWithProvider builder) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
|
||||
return builder
|
||||
.given("test GET")
|
||||
.uponReceiving("GET REQUEST")
|
||||
.path("/pact")
|
||||
.method("GET")
|
||||
.willRespondWith()
|
||||
.status(200)
|
||||
.headers(headers)
|
||||
.body("{\"condition\": true, \"name\": \"tom\"}")
|
||||
.given("test POST")
|
||||
.uponReceiving("POST REQUEST")
|
||||
.method("POST")
|
||||
.headers(headers)
|
||||
.body("{\"name\": \"Michael\"}")
|
||||
.path("/pact")
|
||||
.willRespondWith()
|
||||
.status(201)
|
||||
.toPact();
|
||||
}
|
||||
|
||||
@Test
|
||||
@PactTestFor
|
||||
void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody(MockServer mockServer) {
|
||||
// when
|
||||
ResponseEntity<String> response = new RestTemplate().getForEntity(mockServer.getUrl() + "/pact", String.class);
|
||||
|
||||
// then
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThat(response.getHeaders().get("Content-Type").contains("application/json")).isTrue();
|
||||
assertThat(response.getBody()).contains("condition", "true", "name", "tom");
|
||||
|
||||
// and
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
String jsonBody = "{\"name\": \"Michael\"}";
|
||||
|
||||
// when
|
||||
ResponseEntity<String> postResponse = new RestTemplate().exchange(mockServer.getUrl() + "/pact", HttpMethod.POST, new HttpEntity<>(jsonBody, httpHeaders), String.class);
|
||||
|
||||
// then
|
||||
assertThat(postResponse.getStatusCode().value()).isEqualTo(201);
|
||||
}
|
||||
|
||||
}
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.pact;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
|
||||
import com.baeldung.sampleapp.config.MainApplication;
|
||||
|
||||
import au.com.dius.pact.provider.junit.Provider;
|
||||
import au.com.dius.pact.provider.junit.State;
|
||||
import au.com.dius.pact.provider.junit.loader.PactFolder;
|
||||
import au.com.dius.pact.provider.junit5.HttpTestTarget;
|
||||
import au.com.dius.pact.provider.junit5.PactVerificationContext;
|
||||
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
|
||||
|
||||
@Provider("test_provider")
|
||||
@PactFolder("target/pacts")
|
||||
public class PactProviderLiveTest {
|
||||
|
||||
private static ConfigurableWebApplicationContext application;
|
||||
|
||||
@BeforeAll
|
||||
public static void start() {
|
||||
application = (ConfigurableWebApplicationContext) SpringApplication.run(MainApplication.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void before(PactVerificationContext context) {
|
||||
context.setTarget(new HttpTestTarget("localhost", 8082, "/spring-rest"));
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
@ExtendWith(PactVerificationInvocationContextProvider.class)
|
||||
void pactVerificationTestTemplate(PactVerificationContext context) {
|
||||
context.verifyInteraction();
|
||||
}
|
||||
|
||||
@State("test GET")
|
||||
public void toGetState() {
|
||||
}
|
||||
|
||||
@State("test POST")
|
||||
public void toPostState() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user