与经纪人进行的Springboot协议测试

时间:2020-03-16 17:58:03

标签: spring-boot pact pact-broker

我有一个在AWS云上运行的契约代理。我可以在某些示例网址(例如demo.service.aws.cloud/demo-service/latest)上看到一些描述说“ app-a-service和app-b-service之间的协定”显示了交互。 这是对经纪人的回应:

A pact between express-demo-service and spring-demo-service
Requests from express-demo-service to spring-demo-service

Get test envs given tEST_ENV and SECRET_ENV set
Interactions

Given tEST_ENV and SECRET_ENV set, upon receiving get test envs from express-demo-service, with

{
  "method": "GET",
  "path": "/env",
  "headers": {
    "Authorization": "Bearer "
  }
}
spring-demo-service will respond with:

{
  "status": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": {
    "TEST_ENV": "foo",
    "SECRET_ENV": "bar"
  }
}

现在,我构建我的Springboot rest控制器,该控制器返回文档中定义的那些响应。现在如何进行测试,以确保我的实施符合契约经纪人的要求?

以下是我的TestPacts,以确保我的实现符合经纪人的合同(不确定这是正确的):

@RunWith(PactRunner.class)
@Provider("test_provider" )
@PactBroker(host = "https://mybroker.aws.com/pacts/provider/spring-demo-service/consumer/express-demo-service/latest", port = "80")
@VerificationReports({"console", "markdown"})
public class TestPacts {
    private static final Logger LOG = LoggerFactory.getLogger(TestPacts.class);

    private static ConfigurableApplicationContext application;

    @TestTarget
    public final Target target = new HttpTarget(8080);

    @BeforeClass
    public static void startSpring(){
        LOG.info("starting application");
        application = SpringApplication.run(ProviderServiceApplication.class);
    }

    @State("default")
    public void toDefaultState() {
        LOG.info("Now service in default state");
    }

    @State("extra")
    public void toExtraState() {
        LOG.info("Now service in extra state");
    }

    @AfterClass
    public static void kill(){
        LOG.info("stop application");
        application.stop();
    }
}

我跑步时出现错误:

org.junit.runners.model.InitializationError
    at au.com.dius.pact.provider.junit.PactRunner.initialize(PactRunner.kt:93)
    at au.com.dius.pact.provider.junit.PactRunner.getChildren(PactRunner.kt:140)
    at org.junit.runners.ParentRunner.getFilteredChildren(ParentRunner.java:426)
    at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:351)
    at com.intellij.junit4.JUnit4IdeaTestRunner.getDescription(JUnit4IdeaTestRunner.java:78)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

2 个答案:

答案 0 :(得分:0)

我认为问题可能在于您正在提供pact文件的完整路径,而不仅仅是传递主机

@PactBroker(host = "https://mybroker.aws.com/pacts/provider/spring-demo-service/consumer/express-demo-service/latest", port = "80")

应该是

@PactBroker(host = "https://mybroker.aws.com", port = "443")

另外,该端口对我来说似乎是错误的。 TLS受保护的端点将在端口80上运行是不寻常的-更有可能是443

答案 1 :(得分:0)

尝试使用:

@PactBroker(host = "mybroker.aws.com", scheme = "https", port = "443")

有一个方案属性可以选择http或https。默认情况下,Scheme等于http,当您使用https时,应将其更改为https。