Skip to content

Commit

Permalink
fix: PactVerificationExtension will fail when used with other extensi…
Browse files Browse the repository at this point in the history
…ons in a static context #1666
  • Loading branch information
rholshausen committed Feb 24, 2023
1 parent 3010a10 commit 5bc8bc6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ open class PactVerificationExtension(

override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Boolean {
val store = extensionContext.getStore(ExtensionContext.Namespace.create("pact-jvm"))
val testContext = store.get("interactionContext") as PactVerificationContext
val testContext = store.get("interactionContext") as PactVerificationContext?
return when (parameterContext.parameter.type) {
Pact::class.java -> true
Interaction::class.java -> true
ClassicHttpRequest::class.java, HttpRequest::class.java -> testContext.target is HttpTestTarget
ClassicHttpRequest::class.java, HttpRequest::class.java -> testContext?.target is HttpTestTarget
PactVerificationContext::class.java -> true
ProviderVerifier::class.java -> true
RequestData::class.java -> testContext.target is PluginTestTarget
RequestData::class.java -> testContext?.target is PluginTestTarget
else -> false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package au.com.dius.pact.provider.junit5;

import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.State;
import au.com.dius.pact.provider.junitsupport.loader.PactFolder;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

@Provider("myAwesomeService")
@PactFolder("pacts")
public class WithRegisteredExtensionTest {
private static final Logger LOGGER = LoggerFactory.getLogger(WithRegisteredExtensionTest.class);

@RegisterExtension
static final TestDependencyResolver resolverExt = new TestDependencyResolver(/*...*/);

private final TestDependency dependency;

public WithRegisteredExtensionTest(TestDependency dependency) {
this.dependency = dependency;
}

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void test() {
assertThat(dependency, is(notNullValue()));
}

@State("state 2")
void state2() {
}

@State("default")
void stateDefault() {
}

static class TestDependencyResolver implements Extension, ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getParameter().getType().isAssignableFrom(TestDependency.class);
}

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

static class TestDependency {
}
}

0 comments on commit 5bc8bc6

Please sign in to comment.