当前位置: 首页 > 知识库问答 >
问题:

如何使用JUnit5 spring Boot编写pact-jvm消费者驱动程序pact测试?

白灿
2023-03-14

我是一个新的编写PACT测试用例的人。我有一个非常简单的场景,一个微服务运行在dev服务器上,它根据路径变量返回true或false(这是一个GET调用)。如何编写生成协议测试用例?如何建立一个代理服务器来发布协议?现在我只关心消费者端。

首先,我编写了以下代码?

OrdermsConsumerTest

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "orderms-provider", port = "8888")
public class OrderMsConsumerTest {

    private static final String USER_SUBSCRIPTION_URL = "/api/order/order/subscription?userId=1&userSubscriptionId=1";
    private static final String SERVER_URL = "ACTUAL_SERVER_ADDRESS";

    @BeforeEach
    public void setUp(MockServer mockServer) {
        Assertions.assertTrue(mockServer != null);
    }

    @Pact(state = "user order subscription" , provider = "orderms-provider", consumer="orderms-consumer")
    public RequestResponsePact createPact(PactDslWithProvider builder) {
        Map headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        return builder
                .given("Order User Subscription")
                .uponReceiving("user id and subscription id")
                .path("/api/order/order/subscription?userId=2968&userSubscriptionId=51230")
                .method("GET")
                .willRespondWith()
                .status(200)
                .headers(headers)
                .body("true")
                .toPact();
    }

    @Test
    @PactTestFor(pactMethod = "orderUserSubscription")
    void shouldPassResponseRecivedForGivenUserIdAndSubscriptionId() throws IOException {
        HttpResponse httpResponse = Request.Get(SERVER_URL + USER_SUBSCRIPTION_URL)
                .execute().returnResponse();
        Assertions.assertTrue(httpResponse.getStatusLine().getStatusCode() == 200);
    }
}
java.lang.UnsupportedOperationException: No method annotated with @Pact was found on test class OrderMsConsumerTest for provider 'orderms-provider'

    at au.com.dius.pact.consumer.junit5.PactConsumerTestExt.lookupPact(PactConsumerTestExt.kt:146)
    at au.com.dius.pact.consumer.junit5.PactConsumerTestExt.beforeEach(PactConsumerTestExt.kt:83)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$1(TestMethodTestDescriptor.java:151)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:187)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:187)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachCallbacks(TestMethodTestDescriptor.java:150)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:129)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    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)
    Suppressed: kotlin.TypeCastException: null cannot be cast to non-null type au.com.dius.pact.consumer.junit5.JUnit5MockServerSupport
        at au.com.dius.pact.consumer.junit5.PactConsumerTestExt.afterEach(PactConsumerTestExt.kt:162)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$11(TestMethodTestDescriptor.java:245)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$12(TestMethodTestDescriptor.java:256)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:256)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:255)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:244)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:141)
        ... 41 more


这里我有点困惑@PactTestFor(providerName=“orderms-provider”,端口=“8888”)我现在没有设置代理,所以它会自动运行一个模拟服务器,其提供程序名为orderms-provider,端口为8888?我是否需要在端口8888上运行应用程序并进行实际调用并通过测试?如何使测试用例作为maven构建的一部分运行?

共有1个答案

燕俊明
2023-03-14
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "orderms-provider", port = "8888")
public class OrderMsConsumerTest {

    private static final String USER_SUBSCRIPTION_URL = "/api/order/order/subscription?userId=1&userSubscriptionId=1";
    private static final String SERVER_URL = "ACTUAL_SERVER_ADDRESS";

    @BeforeEach
    public void setUp(MockServer mockServer) {
        Assertions.assertTrue(mockServer != null);
    }

    @Pact(state = "user order subscription" , provider = "orderms-provider", consumer="orderms-consumer")
    public RequestResponsePact orderUserSubscription(PactDslWithProvider builder) {
        Map headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        return builder
                .given("Order User Subscription")
                .uponReceiving("user id and subscription id")
                .path("/api/order/order/subscription?userId=2968&userSubscriptionId=51230")
                .method("GET")
                .willRespondWith()
                .status(200)
                .headers(headers)
                .body("true")
                .toPact();
    }

    @Test
    @PactTestFor(pactMethod = "orderUserSubscription")
    void shouldPassResponseRecivedForGivenUserIdAndSubscriptionId() throws IOException {
        HttpResponse httpResponse = Request.Get(SERVER_URL + USER_SUBSCRIPTION_URL)
                .execute().returnResponse();
        Assertions.assertTrue(httpResponse.getStatusLine().getStatusCode() == 200);
    }
}
 类似资料:
  • 我是新来的。我的项目是Java项目。我通读了pact文档,找到了github项目https://github.com/dius/pact-jvm/tree/master/pact-jvm-consumer-junit,我将其导入到eclipse IDE中。我被困在这里了。1.首先运行哪个测试。ExampleJavaConsumerPactRuleTest还是ExampleJavaConsumerP

  • 我正在使用Pactman和pact-python为CDC测试做一个POC。我可以生成协议文件和验证协议与提供商的基本url而不注册到协议代理,我使用以下方法。它将检查任何失败,这是最好的做法还是我需要使用代理?

  • 我在pact-jvm-consumer中的closeArray有问题。 给定这样的Json,如何构造“dslPart imeiResults=new PactDslJsonBody()”-语句。 我试过这样: 但这不起作用,例如.closearray()不返回PactDslJsonBody,而是返回DslPart,所以在.closearray()之后永远不能有任何东西?我不明白,有人能给我们演示一

  • 我正在执行一些测试,如果使用者设置了某个ID或任何文本,而这些ID或文本在提供者数据库中并不存在,那么我希望在提供者测试中执行以下步骤 接收带有以下信息的协议文件:首先需要设置哪些内容 然后我将有我的函数,它将开始将那些不可用的数据插入数据库 然后调用API以提供实际响应。 现在我想知道,消费者应该使用哪个字段来让提供者知道,在实际的API调用之前需要一些先决条件或预设置。

  • 我正在尝试使用Pact-JVM在我们的两个服务之间生成一个协议。但是当我尝试运行Java类时,我得到了这个异常。