使用 Pact JUnit 规则还是直接使用 Pact DSL?
Using Pact JUnit Rule versus Using the Pact DSL directly?
我想了解为什么会出现以下情况?
问题:如果我使用 Pact Junit 规则,Junit 测试将失败并出现 HttpConnect 异常。但是如果我直接使用 Pact DSL,同样的测试会通过并生成 pact 文件。有人能告诉我为什么以及如何开始使用 Pact Junit 规则吗?
使用 Pact Junit 规则的代码:(因 HttpHostConnectException 而失败)
@Rule
public PactProviderRule rule = new PactProviderRule("DrivePOC", PactSpecVersion.V2, this);
/*Setting up what your expectations are*/
@Pact(provider = "P1",consumer = "C1")
public PactFragment createFragment(PactDslWithProvider builder)
{
PactFragment pactFragment = ConsumerPactBuilder
.consumer("C1")
.hasPactWith("P1")
.uponReceiving("load Files Data")
.path("/loadData")
.method("POST")
.body("{\"givefileId\": \"abc\"}")
.willRespondWith()
.status(200)
.body("{\"fileId\": \"abcfileId1234\"}")
.toFragment();
return pactFragment;
}
/*Test similar to Junit, verifies if the test are ok and the responses are as per expectations set in the createFragment*/
@Test
@PactVerification(value = "P1")
public void runTest() throws IOException
{
MockProviderConfig config = MockProviderConfig.createDefault();
Map expectedResponse = new HashMap();
expectedResponse.put("fileId", "abcfileId1234");
try {
Assert.assertEquals(new ProviderClient(config.url()).helloToDrive("{\"givefileId\": \"abc\"}"),
expectedResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Code using Pact DSL Directly (This Junit passed and generate Pact file successfully)
@Test
public void testPact() {
PactFragment pactFragment = ConsumerPactBuilder
.consumer("C1")
.hasPactWith("P1")
.uponReceiving("load Files Data")
.path("/loadData")
.method("POST")
.body("{\"givefileId\": \"abc\"}")
.willRespondWith()
.status(200)
.body("{\"fileId\": \"abcfileId1234\"}")
.toFragment();
MockProviderConfig config = MockProviderConfig.createDefault();
VerificationResult result = pactFragment.runConsumer(config, new TestRun() {
public void run(MockProviderConfig config) throws Throwable {
Map expectedResponse = new HashMap();
expectedResponse.put("fileId", "abcfileId1234");
try {
Assert.assertEquals(new ProviderClient(config.url()).helloToHueDrive("{\"givefileId\": \"abc\"}"),
expectedResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
if (result instanceof PactError) {
throw new RuntimeException(((PactError)result).error());
}
Assert.assertEquals(ConsumerPactTest.PACT_VERIFIED, result);
}
将 Junit 版本从 4.8 更改为 4.9 后,我可以让我的注释工作。
我想了解为什么会出现以下情况? 问题:如果我使用 Pact Junit 规则,Junit 测试将失败并出现 HttpConnect 异常。但是如果我直接使用 Pact DSL,同样的测试会通过并生成 pact 文件。有人能告诉我为什么以及如何开始使用 Pact Junit 规则吗?
使用 Pact Junit 规则的代码:(因 HttpHostConnectException 而失败)
@Rule
public PactProviderRule rule = new PactProviderRule("DrivePOC", PactSpecVersion.V2, this);
/*Setting up what your expectations are*/
@Pact(provider = "P1",consumer = "C1")
public PactFragment createFragment(PactDslWithProvider builder)
{
PactFragment pactFragment = ConsumerPactBuilder
.consumer("C1")
.hasPactWith("P1")
.uponReceiving("load Files Data")
.path("/loadData")
.method("POST")
.body("{\"givefileId\": \"abc\"}")
.willRespondWith()
.status(200)
.body("{\"fileId\": \"abcfileId1234\"}")
.toFragment();
return pactFragment;
}
/*Test similar to Junit, verifies if the test are ok and the responses are as per expectations set in the createFragment*/
@Test
@PactVerification(value = "P1")
public void runTest() throws IOException
{
MockProviderConfig config = MockProviderConfig.createDefault();
Map expectedResponse = new HashMap();
expectedResponse.put("fileId", "abcfileId1234");
try {
Assert.assertEquals(new ProviderClient(config.url()).helloToDrive("{\"givefileId\": \"abc\"}"),
expectedResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Code using Pact DSL Directly (This Junit passed and generate Pact file successfully)
@Test
public void testPact() {
PactFragment pactFragment = ConsumerPactBuilder
.consumer("C1")
.hasPactWith("P1")
.uponReceiving("load Files Data")
.path("/loadData")
.method("POST")
.body("{\"givefileId\": \"abc\"}")
.willRespondWith()
.status(200)
.body("{\"fileId\": \"abcfileId1234\"}")
.toFragment();
MockProviderConfig config = MockProviderConfig.createDefault();
VerificationResult result = pactFragment.runConsumer(config, new TestRun() {
public void run(MockProviderConfig config) throws Throwable {
Map expectedResponse = new HashMap();
expectedResponse.put("fileId", "abcfileId1234");
try {
Assert.assertEquals(new ProviderClient(config.url()).helloToHueDrive("{\"givefileId\": \"abc\"}"),
expectedResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
if (result instanceof PactError) {
throw new RuntimeException(((PactError)result).error());
}
Assert.assertEquals(ConsumerPactTest.PACT_VERIFIED, result);
}
将 Junit 版本从 4.8 更改为 4.9 后,我可以让我的注释工作。