我开始进行Pact测试,已经进行了Consumer Contract测试并生成JSON pact文件。
我下面的示例有一个运行Pact文件的测试,这是我下面的示例代码,它包含提供程序(bs),使用者(客户端)和验证程序(运行Pact文件){{ 3}}
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>
有什么建议吗?
答案 0 :(得分: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));
}
}