Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when no pacts to verify & test template has also an HttpRequest parameter #1564

Closed
fragonib opened this issue May 27, 2022 · 7 comments

Comments

@fragonib
Copy link

fragonib commented May 27, 2022

When there is no PACTs to verify (that satisfy the constraints) PactVerificationContext context is null. The prefered solution is to check that. But if TestTemplate method has also an HttpRequest parameter like:

    @TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) throws ProtocolException {
        if (context != null) { // Can be null when there are no PACT defined on Broker
            removeContextFromRequestPath(request);
            context.verifyInteraction();
        }
    }

It throws:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.apache.hc.core5.http.HttpRequest request] in method [pactVerificationTestTemplate(au.com.dius.pact.provider.junit5.PactVerificationContext,org.apache.hc.core5.http.HttpRequest) throws org.apache.hc.core5.http.ProtocolException].

When this HttpRequest is removed it logs expected No pact found to verify

Any thoughts?

Originally posted by @fragonib in #768 (comment)

@rholshausen
Copy link
Contributor

The problem is that to know if it can inject an HTTP request, it needs to know the type of the interaction, but there is no Pact files, so it does not know that and there won't be any request object to inject.

@jamescookie
Copy link

We just migrated from pact version 4.2.7 (where it was working fine without any pacts to verify) to 4.4.3 and encountered this problem.

@vghero
Copy link

vghero commented Mar 10, 2023

So what changed from 4.2 to also 4.5 that this can't be detected anymore?

@rholshausen
Copy link
Contributor

With 4.2.x, there was only one type of interaction. With 4.5.x, there can be multiple types of interactions.

@holomekc
Copy link
Contributor

As a workaround I just added this ParameterResolver and registered it via ExtendWith to my test base class:

public class PactNoPactsWorkaround implements ParameterResolver {
    @Override
    public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
            throws ParameterResolutionException {
        final ExtensionContext.Store store = extensionContext.getStore(ExtensionContext.Namespace.create("pact-jvm"));
        final PactVerificationContext testContext = (PactVerificationContext) store.get("interactionContext");
        return testContext == null && parameterContext.getParameter().getType() == HttpRequest.class;
    }

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

@dynnoil
Copy link

dynnoil commented Apr 4, 2024

I used this workaround, maybe it helps someone.

Create Junit5 extension to ignore ParameterResolutionException:

class IgnoreParameterResolutionException implements TestExecutionExceptionHandler {
    @Override
    public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable {
        if (throwable instanceof ParameterResolutionException) {
            return;
        }
        throw throwable;
    }
}

Use it on @TestTemplate method:

@TestTemplate
@ExtendWith({PactVerificationSpringProvider.class, IgnoreParameterResolutionException.class})
void pactVerificationTestTemplate(PactVerificationContext context, MockHttpServletRequestBuilder requestBuilder) {
    requestBuilder
            .with(csrf())
            .contextPath(servletContextPath);
    if (context != null) {
        context.verifyInteraction();
    }
}

@mefellows
Copy link
Member

The recommended way to address this is per the above extension: #1564 (comment)

Closing this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants