marhg
marhg

Reputation: 687

Pact test on Junit5 what to define in @ExtendWith

I am starting with Pact testing, I have already my Consumer contract test and generates the JSON pact file.

The example I am following, has a test that runs the Pact file, Here is the example code I am following, it contains the provider (bs), consumer (client) and verifyer (which runs the Pact file) 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);

}

I want to do the same, but for Junit5, so instead of @RunWith, I need to use @ExtendWith, but what has to be defined inside ExtendWith()?

@ExtendWith(PactRunner.class) does not work, I tried as well with @ExtendWith(PactConsumerTestExt.class) also didn´t work.

In my pom I have:

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

JUnit Jupiter

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

Any suggestions?

Upvotes: 3

Views: 3090

Answers (1)

Mureinik
Mureinik

Reputation: 311228

The PactRunner is a JUnit 4 runner. Instead, you need to use the JUnit 5 extension.

First, you need to add the JUnit 5 extension dependency to your pom.xml. E.g.:

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

Then, you could use the 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));
    }
}

Upvotes: 3

Related Questions