在 Junit5 上进行 Pact 测试 @ExtendWith 中定义的内容

Pact test on Junit5 what to define in @ExtendWith

我开始进行契约测试,我已经进行了消费者契约测试并生成了 JSON 契约文件。

我正在关注的示例有一个运行 Pact 文件的测试,这是我正在关注的示例代码,它包含提供者 (bs)、消费者(客户端)和验证者(运行 Pact 文件) pact example

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,所以我需要使用 @RunWith 而不是 @RunWith ]@ExtendWith,但是在ExtendWith()里面需要定义什么?

@ExtendWith(PactRunner.class) 不起作用,我也尝试过 @ExtendWith(PactConsumerTestExt.class) 也没有用。

在我的 pom 中有:

 <!-- 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>

有什么建议吗?

PactRunner 是 JUnit 4 运行器。相反,您需要使用 JUnit 5 扩展。

首先,您需要将 JUnit 5 扩展依赖项添加到 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));
    }
}