运行 测试时忽略 PACT jvm 匹配规则
PACT jvm matching rules are being ignored when running test
我正在使用 PACT JVM https://github.com/DiUS/pact-jvm/tree/master/provider/pact-jvm-provider-junit
我不知道为什么我的联系人中的匹配规则被忽略了。
我的 HTTP 测试
@RunWith(SpringRestPactRunner.class) // Say JUnit to run tests with custom Runner
@Provider("provDataTest")// Set up name of tested provider
@PactBroker(host="pact-broker.services", port = "80")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = {"spring.profiles.active=dev"},
classes = Application.class)
public class ContractTesting {
private static String token;
@BeforeClass //Method will be run once: before whole contract test suite
public static void setUpService() {
//Run DB, create schema
//Run service
//...
System.out.println("Now service in default state");
}
@TargetRequestFilter
public void exampleRequestFilter(HttpRequest request) {
}
@State("Check correct data json structure in case code: 401")
public void checkDataInCaseUnauthorized() {
}
@TestTarget // Annotation denotes Target that will be used for tests
public final Target target = new HttpTarget(8082);
还有我的合同文件
{
"provider": {
"name": "provDataTest"
},
"consumer": {
"name": "test"
},
"interactions": [
{
"description": "Read all links_401",
"request": {
"method": "GET",
"path": "/v1/consumer/me/link_social_account",
"headers": {
"invalidAuth": "401"
}
},
"response": {
"status": 401,
"headers": {
"Content-Type": "text/json;charset=utf-8"
},
"body": {
"error": {
"code": 401,
"message": "session incorrect",
"errors": []
}
},
"matchingRules": {
"body": {
"$.error": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.error.code": {
"matchers": [
{
"match": "integer"
}
],
"combine": "AND"
},
"$.error.message": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.error.errors[*].message": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.error.errors[*].field": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
}
},
"header": {
"Content-Type": {
"matchers": [
{
"match": "text/json;charset=utf-8",
"regex": "regex"
}
],
"combine": "AND"
}
}
}
},
"providerStates": [
{
"name": "Check correct data json structure in case code: 401"
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "3.2.11"
}
}
}
并在运行
之后显示错误
Read all links_401 returns a response which has a matching body
Read all links_401 returns a response which has a matching
body=BodyComparisonResult(mismatches={/=[BodyMismatch(expected=[B@480e8a7a, actual=[B@10378c35, mismatch=Actual body '[B@10378c35' is not equal to the expected body '[B@480e8a7a', path=/, diff=null)]},
diff=[-{"error":{"code":401,"message":"session incorrect","errors":[]}},
+{"error":{"code":401,"message":"session incorrect, please login again (CR_A1004)","errors":[]}}])
我已经尝试更改正则表达式,但问题仍然存在。
只有消息数据正确才正确。
请帮忙指出我的观点不正确。
谢谢,
问题出在您的 content-type header:"Content-Type": "text/json;charset=utf-8"
。 JSON 有效载荷的正确内容类型应该是 application/json
。这就是没有应用匹配器的原因,因为 body 被视为文本有效负载。只需更改内容类型,然后 body 将被解析为 JSON 文档,匹配器将应用于字段。
我正在使用 PACT JVM https://github.com/DiUS/pact-jvm/tree/master/provider/pact-jvm-provider-junit 我不知道为什么我的联系人中的匹配规则被忽略了。 我的 HTTP 测试
@RunWith(SpringRestPactRunner.class) // Say JUnit to run tests with custom Runner
@Provider("provDataTest")// Set up name of tested provider
@PactBroker(host="pact-broker.services", port = "80")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = {"spring.profiles.active=dev"},
classes = Application.class)
public class ContractTesting {
private static String token;
@BeforeClass //Method will be run once: before whole contract test suite
public static void setUpService() {
//Run DB, create schema
//Run service
//...
System.out.println("Now service in default state");
}
@TargetRequestFilter
public void exampleRequestFilter(HttpRequest request) {
}
@State("Check correct data json structure in case code: 401")
public void checkDataInCaseUnauthorized() {
}
@TestTarget // Annotation denotes Target that will be used for tests
public final Target target = new HttpTarget(8082);
还有我的合同文件
{
"provider": {
"name": "provDataTest"
},
"consumer": {
"name": "test"
},
"interactions": [
{
"description": "Read all links_401",
"request": {
"method": "GET",
"path": "/v1/consumer/me/link_social_account",
"headers": {
"invalidAuth": "401"
}
},
"response": {
"status": 401,
"headers": {
"Content-Type": "text/json;charset=utf-8"
},
"body": {
"error": {
"code": 401,
"message": "session incorrect",
"errors": []
}
},
"matchingRules": {
"body": {
"$.error": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.error.code": {
"matchers": [
{
"match": "integer"
}
],
"combine": "AND"
},
"$.error.message": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.error.errors[*].message": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
},
"$.error.errors[*].field": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
}
},
"header": {
"Content-Type": {
"matchers": [
{
"match": "text/json;charset=utf-8",
"regex": "regex"
}
],
"combine": "AND"
}
}
}
},
"providerStates": [
{
"name": "Check correct data json structure in case code: 401"
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "3.2.11"
}
}
}
并在运行
之后显示错误Read all links_401 returns a response which has a matching body
Read all links_401 returns a response which has a matching
body=BodyComparisonResult(mismatches={/=[BodyMismatch(expected=[B@480e8a7a, actual=[B@10378c35, mismatch=Actual body '[B@10378c35' is not equal to the expected body '[B@480e8a7a', path=/, diff=null)]},
diff=[-{"error":{"code":401,"message":"session incorrect","errors":[]}},
+{"error":{"code":401,"message":"session incorrect, please login again (CR_A1004)","errors":[]}}])
我已经尝试更改正则表达式,但问题仍然存在。 只有消息数据正确才正确。
请帮忙指出我的观点不正确。
谢谢,
问题出在您的 content-type header:"Content-Type": "text/json;charset=utf-8"
。 JSON 有效载荷的正确内容类型应该是 application/json
。这就是没有应用匹配器的原因,因为 body 被视为文本有效负载。只需更改内容类型,然后 body 将被解析为 JSON 文档,匹配器将应用于字段。