Hari Ravi
08/01/2022, 12:24 AMuglyog
Hari Ravi
08/01/2022, 5:41 AMHari Ravi
08/01/2022, 5:41 AM@ExtendWith(PactConsumerTestExt.class)
@ExtendWith(MockitoExtension.class)
@PactTestFor(providerName = "ProviderService")
public class HealthCheckTest {
@Pact(consumer = "Consumer")
public RequestResponsePact getHealthCheck(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/plain");
headers.put("callchainid", "a4275861-f60a-44ab-85a6-c0c2c9df5e27");
return builder
.given("get health check")
.uponReceiving("get health data")
.path("/health")
.method("GET")
.headers(headers )
.willRespondWith()
.status(200)
.body("{\"status\":\"UP\",\"components\":{\"db\":{\"status\":\"UP\",\"details\":{\"database\":\"PostgreSQL\",\"validationQuery\":\"isValid()\"}}}}")
.toPact();
}
@Test
@PactTestFor(pactMethod = "getHealthCheck")
void getHealthData(MockServer mockServer) {
WebClient webClient=WebClient.builder().baseUrl(mockServer.getUrl()).build();
final String callChainId="a4275861-f60a-44ab-85a6-c0c2c9df5e27";
ThreadContext.put(CallChainIdService.HEADER_NAME, callChainId);
AsyncClient asyncClient=new AsyncClient(webClient);
Mono<ClientResponse> responseMono=asyncClient.getHealthCheck();
System.out.println(responseMono);
}
Hari Ravi
08/01/2022, 5:41 AMHari Ravi
08/01/2022, 5:42 AM@Log4j2
@Service
public class AsyncClient implements CMClient {
private final WebClient CLIENT;
public AsyncClient(
@Qualifier("cm-api") WebClient cm
) {
CLIENT = cm;
}
@Override
public Mono<ClientResponse> getHealthCheck() {
return get(MediaType.TEXT_PLAIN, "/health");
}
private Mono<ClientResponse> get(MediaType contentType, String uri, Object... params) {
return CLIENT
.mutate()
.defaultHeader(CallChainIdService.HEADER_NAME, ThreadContext.get(CallChainIdService.HEADER_NAME))
.build()
.get()
.uri(uri, params)
.accept(contentType)
.exchange();
}
}
Hari Ravi
08/01/2022, 5:42 AMHari Ravi
08/01/2022, 5:42 AMau.com.dius.pact.consumer.PactMismatchesException: The following requests were not received:
method: GET
path: /health
query: {}
headers: {callchainid=[a4275861-f60a-44ab-85a6-c0c2c9df5e27], Content-Type=[text/plain]}
matchers: MatchingRules(rules={})
generators: Generators(categories={})
body: MISSING
Hari Ravi
08/01/2022, 5:43 AMHari Ravi
08/01/2022, 5:43 AMHari Ravi
08/01/2022, 5:43 AMString json = WebClient.create()
.get()
.uri(mockServer.getUrl()+"/health")
.exchange()
.block()
.bodyToMono(String.class)
.block();
System.out.println(json);
Hari Ravi
08/01/2022, 5:45 AMuglyog
Hari Ravi
08/01/2022, 6:17 AMHari Ravi
08/01/2022, 6:17 AMau.com.dius.pact.consumer.PactMismatchesException: The following requests were not received:
method: GET
path: /health
query: {}
headers: {callchainid=[a4275861-f60a-44ab-85a6-c0c2c9df5e27], Content-Type=[text/plain]}
matchers: MatchingRules(rules={})
generators: Generators(categories={})
body: MISSING
Hari Ravi
08/01/2022, 6:17 AMuglyog
AsyncClient
is asynchronous. In your example that works, you have calls to .block()
, but in the test you do not.Hari Ravi
08/01/2022, 9:14 PM