Target Server failed to respond
See original GitHub issueHi, I’m currently trying to write a simple pact consumer test with Junit5 & Lambda DSL. I’m stuck with the mock server telling me “Target Server failed to respond”
My versions are: pact-jvm-consumer-junit5 4.0.8 pact-jvm-consumer-java8 4.0.8 Junit Jupiter 5.5.2 Java 11 Kotlin 1.3.70 (I added kotlin-stdlib to my pom.xml with version 1.3.70 for scope test)
I get the following in the logs
07:52:54.773 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Found @PactTestFor annotation on test method 07:52:54.784 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Found @PactTestFor annotation on test class 07:52:54.785 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - providerInfo = ProviderInfo(providerName=contract-service, hostInterface=, port=8080, pactVersion=null, providerType=null) 07:52:54.787 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Looking for @Pact method named 'contract' for provider 'contract-service' 07:52:54.790 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Invoking method 'contract' to get Pact for the test 'testContract' 07:52:54.973 [main] DEBUG au.com.dius.pact.consumer.BaseJdkMockServer - Starting mock server 07:52:54.975 [main] DEBUG au.com.dius.pact.consumer.BaseJdkMockServer - Mock server started: /127.0.0.1:8080 07:52:55.539 [HTTP-Dispatcher] DEBUG au.com.dius.pact.consumer.BaseJdkMockServer - Received request: method: GET path: /contracts/13 query: {} headers: {Accept-encoding=[gzip,deflate], Connection=[Keep-Alive], Host=[127.0.0.1:8080], User-agent=[Apache-HttpClient/4.5.6 (Java/11.0.6)]} matchers: MatchingRules(rules={}) generators: Generators(categories={}) body: EMPTY 07:52:55.542 [HTTP-Dispatcher] DEBUG au.com.dius.pact.core.matchers.RequestMatching - comparing to expected request: method: GET path: /contracts/13 query: {} headers: {} matchers: MatchingRules(rules={}) generators: Generators(categories={}) body: MISSING 07:52:56.072 [HTTP-Dispatcher] DEBUG au.com.dius.pact.core.matchers.Matching - Found a matcher for text/plain -> au.com.dius.pact.core.matchers.PlainTextBodyMatcher@aa4b5e0 07:52:56.073 [HTTP-Dispatcher] DEBUG au.com.dius.pact.core.matchers.RequestMatching - Request mismatch: [] Mar 23, 2020 7:52:56 AM org.apache.http.impl.execchain.RetryExec execute INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://127.0.0.1:8080: The target server failed to respond
My code looks like this:
…
import au.com.dius.pact.consumer.MockServer;
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.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonBody;
import io.pactfoundation.consumer.dsl.LambdaDslJsonBody;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.HashMap;
import java.util.Map;
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "contract-service", port = "8080")
public class PactContractServiceTest {
private final String CONTRACT = "/contracts";
private static final String CONTENT_HEADER = "Content-Type";
private static final String CONTENT_HEADER_VALUE = "application/hal+json";
@Pact(consumer = "mail-service", provider = "contract-service")
public RequestResponsePact contract(PactDslWithProvider builder) {
LambdaDslJsonBody contractEntityJson = newJsonBody((o) -> {
o.id("id");
o.stringType("userId");
… // irrelevant
o.numberType("amount");
});
return builder
.given("Contract with id 13 exists")
.uponReceiving("retrieving contract info")
.path(CONTRACT + "/13")
.method("GET")
.willRespondWith()
.status(200)
.headers(responseHeaders())
.body(contractEntityJson.build())
.toPact();
}
@Test
@PactTestFor(pactMethod = "contract")
public void testContract(MockServer mockServer) throws IOException {
HttpResponse httpResponse = Request.Get(mockServer.getUrl() + CONTRACT + "/13").execute().returnResponse();
assertEquals(httpResponse.getStatusLine().getStatusCode(), 200);
}
private Map<String, String> responseHeaders() {
Map<String, String> headers = new HashMap();
headers.put(CONTENT_HEADER, CONTENT_HEADER_VALUE);
return headers;
}
}
I assume I’m either missing something very basic, or there’s a problem with the versions I use?
Any help is highly appreciated!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
I think I fixed it by updating gson to 2.8.6 (I used 2.8.5 before) – I think this should be documented somewhere though (or work out of the box 😉 ).
Closing this as it seems to always be due to outdated dependencies causing the issue.