生成的测试(生产方)在 Spring 云合同和 AMQP/RabbitMQ 中失败

The generated tests(producer side) failed in Spring Cloud contract and AMQP/RabbitMQ

我试图在我的微服务项目中使用 AMQP/RabbitMQ 和 Spring 云合同来定义生产者和消费者之间的合同。

我正在使用最后一个 Spring Boot 2.0.3,和 Spring Cloud Contract 2.0.0。

我准备了一个示例项目来重现该问题。

生产者端,我创建了一个基础测试class:

@SpringBootTest(properties = "stubrunner.amqp.enabled=true")
@RunWith(SpringRunner.class)
@AutoConfigureMessageVerifier
public class MessageVerifierBase {

    @Autowired
    MessageVerifier verifier;

    @Autowired
    Sender sender;

    public void send() {
        this.sender.send(Notification.builder().body("test message").build());
    }

    @Before
    public void setup() {
        verifier.receive("notification.exchange", 100, TimeUnit.MILLISECONDS);
    }
}

Sender刚刚调用了RabbitTemplate

    @Component
public class Sender {

    @Autowired
    AmqpTemplate amqpTemplate;

    public void send(Notification notification){
        this.amqpTemplate.convertAndSend("notification.exchange", "notification.messages", notification);
    }

}

并以 groovy 格式创建合同。

    org.springframework.cloud.contract.spec.Contract.make {
    description("""
        send messages by rabbitmq
    """)
    label "notification.event"
    // input to the contract
    input {
        // the contract will be triggered by a method
        triggeredBy('send()')
    }
    outputMessage {
        sentTo "notification.exchange"
        body([
            body: "test message",
            type: "MESSAGE"
        ])
        headers {
            header("contentType", applicationJsonUtf8())
            header("__TypeId__", "com.example.demo.Notification")
        }
    }
}

我运行mvn clean install的时候失败了,报如下问题:

    Wanted but not invoked:
rabbitTemplate.send(
    "notification.exchange",
    <Capturing argument>,
    <Capturing argument>,
    <any org.springframework.amqp.rabbit.support.CorrelationData>
);
-> at org.springframework.cloud.contract.verifier.messaging.amqp.SpringAmqpStubMessages.receive(SpringAmqpStubMessages.java:110)

However, there were exactly 3 interactions with this mock:
rabbitTemplate.getMessageConverter();
-> at org.springframework.amqp.rabbit.core.RabbitMessagingTemplate.afterPropertiesSet(RabbitMessagingTemplate.java:111)

rabbitTemplate.getMessageConverter();
-> at org.springframework.amqp.rabbit.core.RabbitMessagingTemplate.afterPropertiesSet(RabbitMessagingTemplate.java:113)

rabbitTemplate.getMessageConverter();
-> at org.springframework.cloud.contract.verifier.messaging.amqp.ContractVerifierAmqpAutoConfiguration.contractVerifierMessaging(ContractVerifierAmqpAutoConfiguration.java:82)

我刚刚解决了这个问题 - https://github.com/spring-cloud/spring-cloud-contract/issues/676。问题在于 Mockito 迁移(以及我添加的 2 个缺失测试)。因此,Mockito 验证传递的对象是否属于给定的 class 改变了它的行为。在以前的版本中它接受空值,现在不接受。缺少验证 null 的测试,否则,显然我们会更早地发现它。感谢报告,感谢样品。