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

时间:2018-06-29 10:15:05

标签: spring rabbitmq spring-amqp spring-cloud-contract

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

我正在使用最新的Spring Boot 2.0.3和Spring Cloud Contract 2.0.0。

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

生产者方面,我创建了一个基础测试类:

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

}

并以常规格式创建合同。

    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)

1 个答案:

答案 0 :(得分:0)

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