Skip to content

Commit

Permalink
fix: Use context.testMethod instead of context.requiredTestMethod #1643
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jan 4, 2023
1 parent 0423fda commit 4a5ff59
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,10 @@ class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCal
val providerInfo = when {
store["providers"] != null -> store["providers"] as List<Pair<ProviderInfo, String>>
else -> {
val methodAnnotation = if (AnnotationSupport.isAnnotated(context.requiredTestMethod, PactTestFor::class.java)) {
logger.debug { "Found @PactTestFor annotation on test method ${context.requiredTestMethod}" }
AnnotationSupport.findAnnotation(context.requiredTestMethod, PactTestFor::class.java).get()
val methodAnnotation = if (context.testMethod.isPresent && AnnotationSupport.isAnnotated(context.testMethod.get(), PactTestFor::class.java)) {
val testMethod = context.testMethod.get()
logger.debug { "Found @PactTestFor annotation on test method $testMethod" }
AnnotationSupport.findAnnotation(testMethod, PactTestFor::class.java).get()
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package au.com.dius.pact.consumer.junit5;

import au.com.dius.pact.consumer.MessagePactBuilder;
import au.com.dius.pact.consumer.dsl.LambdaDsl;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.annotations.Pact;
import au.com.dius.pact.core.model.messaging.Message;
import au.com.dius.pact.core.model.messaging.MessagePact;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.*;

import java.util.List;
import java.util.Objects;

@ExtendWith({PactConsumerTestExt.class, SamplePactTest.MyStringResolverExtension.class})
@PactTestFor(providerName = "some-provider", providerType = ProviderType.ASYNCH, pactVersion = PactSpecVersion.V3)
class SamplePactTest {

@BeforeAll
static void setup(String injectedString) {
System.out.println(injectedString);
}

@Pact(consumer = "some-consumer")
public MessagePact someMessage(MessagePactBuilder builder) {
return builder
.expectsToReceive("message")
.withContent(LambdaDsl.newJsonBody(object -> object.stringType("test", "Test")).build()).toPact();
}

@Test
@PactTestFor(pactMethod = "someMessage")
void consume(List<Message> messages) {
System.out.println(messages);
}

static class MyStringResolverExtension implements Extension, ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return Objects.equals(parameterContext.getParameter().getType(), String.class);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return "injectedString";
}
}
}

0 comments on commit 4a5ff59

Please sign in to comment.