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

Junit5上的Pact测试在@ExtendWith中定义什么

子车鸿运
2023-03-14

我从Pact测试开始,我已经有了我的消费者契约测试并生成了JSON Pact文件。

我下面的例子有一个运行Pact文件的测试,下面是我下面的示例代码,它包含提供者(bs)、使用者(客户端)和验证者(运行Pact文件)Pact示例

import au.com.dius.pact.provider.junit.PactRunner;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.State;
import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit.target.HttpTarget;
import au.com.dius.pact.provider.junit.target.Target;
import au.com.dius.pact.provider.junit.target.TestTarget;
import org.junit.runner.RunWith;

@RunWith(PactRunner.class) 
@Provider("BusService") 
@PactFolder("../pacts")

public class BusStopContractTest {

    @State("There is a bus with number 613 arriving to Hammersmith bus station") 
    public void hammerSmith() {
        System.out.println("There is a bus with number 613 arriving to Hammersmith bus station" );
    }


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

}

我也想这样做,但是对于Junit5,所以我需要使用@ExtendWith,而不是@runWith,但是在ExtendWith()中必须定义什么?

 <!-- Pact Provider-->
    <dependency>
      <groupId>au.com.dius</groupId>
      <artifactId>pact-jvm-provider-junit_2.12</artifactId>
      <version>3.5.24</version>
    </dependency>

Junit木星

<groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <scope>test</scope>
</dependency>

有什么建议吗?

共有1个答案

赵健柏
2023-03-14

pactrunner是一个JUnit4运行程序。相反,您需要使用JUnit5扩展。

首先,您需要将JUnit5扩展依赖项添加到pom.xml中。例如:

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-provider-junit5_2.12</artifactId>
    <version>3.5.24</version>
</dependency>

然后,可以使用PactVerificationInvocationContextProvider:

@ExtendWith(PactVerificationInvocationContextProvider.class)
@Provider("BusService") 
@PactFolder("../pacts")
public class BusStopContractTest {

    @State("There is a bus with number 613 arriving to Hammersmith bus station") 
    public void hammerSmith() {
        System.out.println("There is a bus with number 613 arriving to Hammersmith bus station" );
    }

    // A @BeforeEach method with an injected PactVerificationContext replaces
    // the old method annotated with @TestTarget
    @BeforeEach
    void setUpTarget(PactVerificationContext context) {
      context.setTarget(new HttpTarget(8111));
    }
}
 类似资料:
  • 我在试着采纳协议。我了解消费者方面的等式,它看起来非常好。但我对制作人方面感到困惑。 文档似乎提倡运行提供者应用程序,并根据正在运行的服务器验证合同。 我怎样才能用Pact实现这一点呢?

  • 我是一个新的编写PACT测试用例的人。我有一个非常简单的场景,一个微服务运行在dev服务器上,它根据路径变量返回true或false(这是一个GET调用)。如何编写生成协议测试用例?如何建立一个代理服务器来发布协议?现在我只关心消费者端。 首先,我编写了以下代码? OrdermsConsumerTest 这里我有点困惑@PactTestFor(providerName=“orderms-provi

  • 我有以下简单的类 和以下配置文件 原因:java.lang.ClassNotFoundException:org.springframework.core.annotation.mergedannotations$searchStrategy at java.base/jdk.internal.loader.builtInclassLoader.LoadClass(builtInclassLoad

  • 我正在寻找一种有效地针对JUnit5集成测试的方法。测试使用gradle运行,所有集成测试类都由自定义类级别注释修饰。我可以根据它们的类名模式或通过使用标记对它们进行分类来筛选它们,但是有没有一种方法可以设置一个针对带有注释的测试的测试套件呢?

  • 我用Spring创建了一个REST服务。现在我想使用pact和JUnit5来测试消费者-提供者-通信。我已经有一个正在运行的pact-broker(来自https://github.com/jaimeniswonger/pact-broker-openshift)。消费者测试工作良好,并公布了协议。provider-test加载这些并尝试验证它们。但测试仅在验证成功时才上载结果。我尝试用suref

  • 尝试编写我的第一个pact测试,但我无法找到基本问题的答案。使用者测试和提供者测试是仅针对模拟服务器运行,还是我们应该在本地(或在CI/CD期间的特定环境中)构建应用程序,然后针对实际运行的应用程序运行测试?另外,我是否可以对模拟服务器运行使用者测试,而对实际httpsendpoint运行提供者测试?