Francislainy Campos
Francislainy Campos

Reputation: 4164

How to verify PACT test locally before the contract is published to the online broker?

I have a situation where I've access to a contract as a .json file but it's not published yet and it may take some time for this to happen. I want to start writing the verification tests for it, so was thinking of pasting this file manually under the pact/folder and connect to it rather than an online broker? I think I've seen something like this done somewhere before. Is this really possible? If so, could I get a sample please? Thanks very much.

Upvotes: 0

Views: 1636

Answers (1)

Francislainy Campos
Francislainy Campos

Reputation: 4164

Answering my own question here. It's possible by either generating or manually pasting the .json file created by the consumer under the target/pacts folder and adding the @PactFolder("target/pacts") annotation as a replacement for the broker path.

enter image description here

import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.State;
import au.com.dius.pact.provider.junit5.HttpsTestTarget;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import org.apache.http.HttpRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;


import static com.hmhco.cp.config.Constants.*;

@Provider("UPDATE")
@PactFolder("target/pacts")
public class PactProviderTest {

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
    request.addHeader("Authorization", AUTHORIZATION_TOKEN);
    context.verifyInteraction();
}

@BeforeEach
void before(PactVerificationContext context) {
    context.setTarget(new HttpsTestTarget(BASE_URL, 443, "/update/v1/lastUpdateSummary"));
}

@State("v1/lastUpdateSummary")
public void sampleState() {
    getAuthorizationToken("teacher");
}

}

PS: Make sure this is the import used:

import au.com.dius.pact.provider.junit.loader.PactFolder;

Upvotes: 4

Related Questions