如何使用pactverificationspringprovider和junit5为pact提供者测试添加头文件?

r6hnlfcb  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(463)

我正在尝试使用junit5spring编写一个pact提供者测试。
目前我得到了一个例外:

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

如何添加标题?我已经试过了:

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

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

但它也不起作用:

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)].

全班:

@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));
    }

}
hsgswve4

hsgswve41#

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

此消息来自提供者端的响应验证。它告诉pact希望服务器返回 Content-Type 头和它不。您的应用程序应该根据这个和测试进行调整,即。 @RestController 或者别的什么:

@RequestMapping(value = "/my/url", produces = { "application/json" }

相关问题