mercredi 7 avril 2021

How to add a header for a pact provider test using PactVerificationSpringProvider and Junit5?

I am trying to write a pact provider test using junit5spring .

Currently I get the exception:

1) Verifying a pact between external-service and my-app - includes headers "Content-Type" with value "[application/json]"

    1.1) header: Expected a header 'Content-Type' but was missing

How should I add the header? I already tried like this:

@TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    public void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
        request.addHeader("Content-Type", "application/json");

        if (context != null) {
            context.verifyInteraction();
        }
    }

but it also does not work:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.apache.http.HttpRequest request] in method [public void com.swisscom.abusehq.anonymizer.pact.PactProviderTest.pactVerificationTestTemplate(au.com.dius.pact.provider.junit5.PactVerificationContext,org.apache.http.HttpRequest)].

Whole class:

@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ContextConfiguration(classes = {MyTestConfiguration.class})
@Provider("my-app")
@PactBroker
        (
                consumerVersionSelectors = @VersionSelector(tag = "developer-build"),
                authentication = @PactBrokerAuth(
                        username = "${pactbroker.auth.username}",
                        password = "${pactbroker.auth.password}"
                )
        )
@Slf4j
public class PactProviderTest {

    @InjectMocks
    MyController myController;

    @Mock
    MyService myService;

    @TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    public void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
        request.addHeader("Content-Type", "application/json");

        if (context != null) {
            context.verifyInteraction();
        }
    }

    @BeforeEach
    void before(PactVerificationContext context) {
        MockitoAnnotations.initMocks(this);
        MockMvcTestTarget target = new MockMvcTestTarget();
        target.setControllers(myController);
        context.setTarget(target);
    }

    @State("account exists")
    public void notificationForAccount(Map data) {
        log.info("Now service in 'state 1' state: " + data);

        doNothing().when(myService).sendNotification(any(MyRequest.class));
    }

}

Aucun commentaire:

Enregistrer un commentaire