我正在尝试使用 pact-JVM 在我们的两个服务之间生成一个协议。但是当我尝试运行 Java 类时,我得到了这个异常。
1) 我怀疑契约体有问题,对吗?在 PactDslWithProvider 的 JSON 正文中有一个额外的“消息”参数,但在 runTest1() 方法中,我仅等同于列表,当我检查结果时,它们对我来说是相同的。2) 在 runTest1() 方法中提供实际的提供者 URL 是否正确?(提供者已经到位)
au.com.dius.pact.consumer.PactMismatchesException: The following requests were not received:
method: GET
path: /devices/v1
query: [externalId:[0942dc67-35de-44f7-a061-743f59436a98]]
headers: [:]
matchers: MatchingRules(rules=[:])
generators: Generators(categories={})
body: OptionalBody(state=MISSING, value=null)
下面是我的Java类
public class PactForDevice {
Map<String, String> headers = MapUtils.putAll(new HashMap<String, String>(), new String[]{"Content-Type", "application/json;charset=UTF-8"});
@Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("device-service-m", this);
@Pact(consumer = "device-r", provider = "device-service-m")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
return builder
.given("Device M details")
.uponReceiving("retrieving Device details")
.path("/devices/v1")
.method("GET")
.query("externalId=0942dc67-35de-44f7-a061-743f59436a98")
.willRespondWith()
.headers(headers)
.status(200)
.body("{" +
"\"data\": [,\n " +
"{ \n" +
" \"dateRegistered\": \"2017-07-13T11:10:51.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"a02b14ee72192ab3\",\n" +
" \"description\": \"Samsung SM-G930F\",\n" +
" \"title\": \"a02b14ee72192ab3\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
"},\n" +
"{\n" +
" \"dateRegistered\": \"2017-07-13T10:45:51.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"a41c3af56ec35874\",\n" +
" \"description\": \"Samsung SM-T819\",\n" +
" \"title\": \"a41c3af56ec35874\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
" },\n" +
" {\n" +
" \"dateRegistered\": \"2017-07-13T10:45:31.000+12:00\",\n" +
" \"alias\": \"\",\n" +
" \"id\": \"bd2b027bbd0a2f17\",\n" +
" \"description\": \"Samsung SM-A320Y\",\n" +
" \"title\": \"bd2b027bbd0a2f17\",\n" +
" \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
" }\n" +
"],\n" +
" \"message\": \"3 devices found for the user 0942dc67-35de-44f7-a061-743f59436a98\"\n" +
"}")
.toPact();
}
@PactVerification("device-service-m")
@Test
@JsonIgnoreProperties(ignoreUnknown = true)
public void runTest1() throws IOException {
final GetDevicesResponse deviceResponse = new GetDevicesResponse();
final List<Device> deviceList = new ArrayList<>();
Device dev = new Device();
dev.withDateRegistered("2017-07-13T11:10:51.000+12:00");
dev.withAlias("");
dev.withId("a02b14ee72192ab3");
dev.withDescription("Samsung SM-G930F");
dev.withTitle("a02b14ee72192ab3");
dev.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev);
Device dev1 = new Device();
dev1.withDateRegistered("2017-07-13T10:45:51.000+12:00");
dev1.withAlias("");
dev1.withId("a41c3af56ec35874");
dev1.withDescription("Samsung SM-T819");
dev1.withTitle("a41c3af56ec35874");
dev1.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev1);
Device dev2 = new Device();
dev2.withDateRegistered("2017-07-13T10:45:31.000+12:00");
dev2.withAlias("");
dev2.withId("bd2b027bbd0a2f17");
dev2.withDescription("Samsung SM-A320Y");
dev2.withTitle("bd2b027bbd0a2f17");
dev2.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
deviceList.add(dev2);
deviceResponse.setDevices(deviceList);
final RestTemplate restTemplate = new RestTemplate();
GetDevicesResponse devices = restTemplate.getForObject("http://localhost:8091/devices/v1?externalId=0942dc67-35de-44f7-a061-743f59436a98", GetDevicesResponse.class);
assertThat(devices, sameBeanAs(deviceResponse));
}
}
编辑:
我刚刚发现,如果我注释掉 @Rule 部分,测试就会通过 - 但不会生成协议文件。我应该为此明确指定一个“协议”文件夹吗?