未检测到契约测试PactProviderRule
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未检测到契约测试PactProviderRule相关的知识,希望对你有一定的参考价值。
我开始进行Pact测试,然后按照Pact-JVM-Example中的示例进行操作,然后创建了自己的测试
生产者从用户那里返回数据(非常简单的JSON){“ id”:1“ firstName”:“名称”,“ lastName”:“姓氏”}
消费者测试是:
public class HelloControllerTest {
@Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("DemoService", "localhost", 8112, this);
@Pact(consumer = "DemoClient")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap();
headers.put("Content-Type", "application/json");
DslPart userResult = new PactDslJsonBody()
.integerType("id",1)
.stringType("fistName","name")
.stringType("lastName","last")
.asBody();
return builder
.given("There is a user with Id 1")
.uponReceiving("A request for user 1")
.path("/user/1")
.method("GET")
.willRespondWith()
.status(200)
.headers(headers)
.body(userResult).toPact();
}
@Test
@PactVerification()
public void doTest() {
HelloController helloController = new HelloController(provider.getPort());
}
}
并且Consumer类看起来像:
public class HelloController {
int port = 8200;
HelloController(){
// Will use default port.
System.out.println("Default port "+ port);
}
HelloController(int port){
this.port = port;
System.out.println("Custom port "+ port);
}
public boolean getUser(){
try {
String url=String.format("http://localhost:%d/user/%d", port, 1);
System.out.println("using url: " + url);
HttpResponse r = Request.Get(url).execute().returnResponse();
String json = EntityUtils.toString(r.getEntity());
System.out.println("json = " + json);
return true;
}
catch (Exception e) {
System.out.println("Unable to get user");
return false;
}
}
}
我注意到当我运行@Test时:
@Test
@PactVerification()
public void doTest() {
HelloController helloController = new HelloController(provider.getPort());
}
我收到了NullPointerException。
测试未从@Rule中声明的提供程序获取模拟数据
@Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("DemoService", "localhost", 8112, this);
我不知道缺少什么,为什么不检测Mock提供程序?
任何建议,谢谢
答案
您必须在测试方法上注明您的契约片段。
@PactVerification(fragment = "createFragment")
以上是关于未检测到契约测试PactProviderRule的主要内容,如果未能解决你的问题,请参考以下文章