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

标签 java junit junit5 pact

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

我下面的示例有一个运行 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,所以我需要使用 @ExtendWith,而不是 @RunWith,但是什么必须在 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>

朱尼特木星

<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));
    }
}

关于java - 在 Junit5 上进行 Pact 测试 @ExtendWith 中定义的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041490/

相关文章:

java - 如何匹配重复的图案?

java - 转义字符 "|"错误(Linux命令)

Play 框架中的 java.lang.NoSuchMethodError Rest Assured 异常

java - Junit 5与Spring Boot : When to use @ExtendWith Spring or Mockito?

java - Spring 中 @Transactional 相对于 ProxyFactoryBean 的性能

java - JUnit 询问用户测试是否成功?

java - 测试我的 Maven 库与 Android 的兼容性

java - 使用 Jupiter 控制台启动器运行 jqwik 测试

java - JUnit测试类顺序

java无符号字节流