Hi Team, Please let me know does PACT Mock Server ...
# pact-jvm
h
Hi Team, Please let me know does PACT Mock Server support spring webclient ?
u
Depends on what you mean by "support spring webclient". The mock server is just an HTTP server that gets it's behaviour from a Pact file.
h
Here is my example which is no working on WebClient
Copy code
@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);


    }
Client Class:
Copy code
@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();
  }
  
}
when i run the test , it says no one is calling the mock
Copy code
au.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
when i run with the debugger, i notice mockserver url got passed and some how the mock server unable to match the health check call
when i use webClient to directly call the health endpoint in my test class i got the response..
Copy code
String json = WebClient.create()
                .get()
                .uri(mockServer.getUrl()+"/health")
                .exchange()
                .block()
                .bodyToMono(String.class)
                .block();
        System.out.println(json);
My eyes are going to it pact mockserver does it has support for webclient
u
Check the logs, you will see all the requests the mock server received
h
I am confused.. when i run the test, the log shows “he following requests were not received”
Copy code
au.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
are you talking about this log or different one
u
I assume
AsyncClient
is asynchronous. In your example that works, you have calls to
.block()
, but in the test you do not.
h
thanks .. this helps