![](/img/trans.png)
[英]Unable to run JUnit5 PACT test. No method annotated with @Pact was found on test class ConsumerContractTest for provider ''
[英]Basic Pact/Junit5 Test Setup fails. No method annotated with @Pact was found for provider error
我尝试按照 Pact.io 上的文档编写一个简单的集成测试。 不幸的是,我得到一个例外如下:
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'
它说我没有任何用@Pact 注释的方法。 但是我确实有一个方法,它用@Pact 注释。
我尝试手动运行测试并使用“mvn test”。
该应用程序通常提供一些 Rest 控制器,应进行测试。
以下是我已经实施的关于我的 Pact 测试实施的所有内容。 我错过了什么吗?
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;
}
}
似乎带有@Pact注释的方法名称必须与@PactTestFor注释中的pactMethod相同......
就我而言,我必须写以下内容:
@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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.