【问题标题】:Pact test on Junit5 what to define in @ExtendWithJunit5 上的 Pact 测试在 @ExtendWith 中定义什么
【发布时间】:2019-05-08 13:04:16
【问题描述】:

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

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

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>

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>

有什么建议吗?

【问题讨论】:

    标签: java junit junit5 pact


    【解决方案1】:

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

    【讨论】:

    • 感谢您的快速回复,我认为拥有“jupiter 依赖项”(我只是将依赖项作为参考)就足够了,但我没有t know that also I have to include junit5 it in the pact dependencies. Can I remove the jupiter dependencies? I have made the changes, but I cant 运行测试类,它没有“运行”选项
    • @andreahg 您仍然需要 jupiter 依赖项才能拥有基本的 Jupiter 功能。您还应该检查您的 Maven Surefire 版本 - 旧版本无法立即运行 Jupiter 测试。
    • 让我检查一下,这可能是测试类无法运行的原因?
    • 我可以在@ExtendWith 中使用另一个类吗,因为 PactVerificationInvocationContextProvider.class 不会使该类可运行
    • 您好,PactVerification 类来自&lt;dependency&gt; &lt;groupId&gt;au.com.dius&lt;/groupId&gt; &lt;artifactId&gt;pact-jvm-provider-junit5&lt;/artifactId&gt; &lt;version&gt;4.0.10&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt;
    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2021-12-29
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多