首页 > 解决方案 > 生产者的合同消息(kafka)测试期望密钥存在,否则为空指针

问题描述

在从spring-cloud-contract-maven-plugin生成的ContractVerifierTest中,密钥是预期的,否则我会得到 nullpointer 异常。我使用 kafkaTemplate.send(topic-name, pojo),故意省略了分区和密钥。

SuppressWarnings("rawtypes")
public class ContractVerifierTest extends BaseClass {
    @Inject ContractVerifierMessaging contractVerifierMessaging;
    @Inject ContractVerifierObjectMapper contractVerifierObjectMapper;

    @Test
    public void validate_shouldSendFoo() throws Exception {
        // when:
            trigger();

        // then:
            ContractVerifierMessage response = contractVerifierMessaging.receive("test-topic",
                    contract(this, "shouldSendFoo.yml"));
            assertThat(response).isNotNull();

        // and:
            DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.getPayload()));
            assertThatJson(parsedJson).field("['specversion']").isEqualTo("1.0");
            assertThatJson(parsedJson).field("['type']").isEqualTo("");

... assertions left out
...

如果消息在没有密钥的情况下发送,则生成的测试将失败并出现 NullPointer 异常。

对此有什么建议吗?

标签: spring-cloud-contract

解决方案


推荐阅读