Reputation: 705
I tried to follow the documentation on Pact.io to write a simple integration test. Unfortunately i get an exception as follows:
org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [au.com.dius.pact.consumer.MockServer mockServer] in method [public void com.example.demo.integration.pact.PactTest.setUp(au.com.dius.pact.consumer.MockServer)]: No method annotated with @Pact was found on test class PactTest for provider 'node_server'
It says that I don't have any method annotated with @Pact. However I do have a method, which is annotated with @Pact.
I tried to run the test manually and with 'mvn test' as well.
The application in general is providing some Rest Controllers, which should be tested.
Following is all I have implemented regarding my Pact Test Implementation. Am I missing something?
package com.example.demo.integration.pact;
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 org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = PactTest.PACT_PROVIDER_NAME)
public class PactTest {
public static final String PACT_PROVIDER_NAME = "node_server";
public static final String PACT_CONSUMER_NAME = "spring_application";
@BeforeEach
public void setUp(MockServer mockServer) {
System.out.println("Mockserver check called");
Assertions.assertTrue(mockServer != null);
}
@Pact(provider = PACT_PROVIDER_NAME, consumer = PACT_CONSUMER_NAME)
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.uponReceiving("notes")
.path("/notes")
.method("GET")
.willRespondWith()
.matchHeader("Content-Type","application/json")
.status(200)
.body(
getJsonArrayOfNotes(2).toString())
.toPact();
}
@Test
@PactTestFor(pactMethod = "notes")
void test(MockServer mockServer) throws IOException {
HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/notes").execute().returnResponse();
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
assertEquals(getJsonArrayOfNotes(2).toString(),httpResponse.getEntity().getContent().toString());
}
private JSONArray getJsonArrayOfNotes(int size) {
var responseJsonObject = new JSONArray();
for (int i = 0; i < size; i++) {
var note = new JSONObject();
try {
note.put("title", String.format("Title %s", i + 1));
note.put("content", String.format("Some Note Content of Note %s", i + 1));
} catch (Exception exception) {
}
responseJsonObject.put(note);
}
return responseJsonObject;
}
}
Upvotes: 1
Views: 3920
Reputation: 705
Seems like the method name with the @Pact annotation must be the same as the pactMethod in the @PactTestFor annotation...
In my case I had to write following:
@Test
@PactTestFor(pactMethod = "getNotes")
void test(MockServer mockServer) throws IOException {
HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/notes").execute().returnResponse();
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
assertEquals(getJsonArrayOfNotes(2).toString(),httpResponse.getEntity().getContent().toString());
}
@Pact(provider = PACT_PROVIDER_NAME, consumer = PACT_CONSUMER_NAME)
public RequestResponsePact getNotes(PactDslWithProvider builder) {
return builder
.uponReceiving("notes")
.path("/notes")
.method("GET")
.willRespondWith()
.matchHeader("Content-Type","application/json")
.status(200)
.body(
getJsonArrayOfNotes(2).toString())
.toPact();
}
Upvotes: 3