Last active
December 20, 2022 13:36
-
-
Save dnno/2cbbbe4db0c89412e69021621aaf1378 to your computer and use it in GitHub Desktop.
PactProviderTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
public class PactProviderTest { | |
ProviderInfo providerInfo; | |
ConsumerInfo consumerInfo; | |
static Pact consumerPact; | |
@LocalServerPort | |
int port; | |
@BeforeEach | |
void setup() { | |
providerInfo = new ProviderInfo("UserService"); | |
providerInfo.setProtocol("http"); | |
providerInfo.setHost("localhost"); | |
providerInfo.setPort(port); | |
providerInfo.setPath("/"); | |
consumerInfo = new ConsumerInfo("UserClient"); | |
consumerInfo.setName("consumer_client"); | |
consumerInfo.setPactSource(new FileSource(new File("build/pacts/UserConsumer-UserService.json"))); | |
//noinspection ConstantConditions | |
consumerPact = DefaultPactReader.INSTANCE.loadPact(consumerInfo.getPactSource()); | |
} | |
@Test | |
void runConsumerPacts() { | |
// grab the first interaction from the pact with consumer | |
Interaction interaction = consumerPact.getInteractions().get(0); | |
// setup the verifier | |
ProviderVerifier verifier = setupVerifier(interaction, providerInfo, consumerInfo); | |
// setup any provider state | |
// setup the client and interaction to fire against the provider | |
ProviderClient client = new ProviderClient(providerInfo, new HttpClientFactory()); | |
Map<String, Object> failures = new HashMap<>(); | |
//noinspection ConstantConditions | |
VerificationResult result = verifier.verifyResponseFromProvider( | |
providerInfo, | |
interaction.asSynchronousRequestResponse(), | |
interaction.getDescription(), | |
failures, | |
client); | |
if (!(result instanceof VerificationResult.Ok)) { | |
verifier.displayFailures(List.of((VerificationResult.Failed) result)); | |
} | |
assertThat(result).isInstanceOf(VerificationResult.Ok.class); | |
} | |
private ProviderVerifier setupVerifier(Interaction interaction, ProviderInfo provider, ConsumerInfo consumer) { | |
ProviderVerifier verifier = new ProviderVerifier(); | |
verifier.initialiseReporters(provider); | |
if (!interaction.getProviderStates().isEmpty()) { | |
for (ProviderState providerState : interaction.getProviderStates()) { | |
//noinspection ConstantConditions | |
verifier.reportStateForInteraction(providerState.getName(), provider, consumer, true); | |
} | |
} | |
verifier.reportInteractionDescription(interaction); | |
return verifier; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment