在Spring云契约和AMQP/RabbitMQ中生成的测试(生产者端)失败
我试图在我的微服务项目中使用AMQP/RabbitMQ和Spring云契约来定义生产者和消费者之间的契约 我使用的是上一代Spring Boot 2.0.3和Spring Cloud Contract 2.0.0 我准备了一个样本项目来重现这个问题在Spring云契约和AMQP/RabbitMQ中生成的测试(生产者端)失败,spring,rabbitmq,spring-amqp,spring-cloud-contract,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) @AutoConfig
@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
刚刚调用了rabbitmplate
@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)
需要但未调用:
拉比泰姆普拉特(
“通知.交换”,
,
,
);
->位于org.springframework.cloud.contract.verifier.messaging.amqp.SpringAmqpStubMessages.receive(SpringAmqpStubMessages.java:110)
但是,与此模拟有3次交互:
rabbitTemplate.getMessageConverter();
->位于org.springframework.amqp.rabbit.core.RabbitMessagingTemplate.AfterPropertieSet(RabbitMessagingTemplate.java:111)
rabbitTemplate.getMessageConverter();
->位于org.springframework.amqp.rabbit.core.RabbitMessagingTemplate.AfterPropertieSet(RabbitMessagingTemplate.java:113)
rabbitTemplate.getMessageConverter();
->在org.springframework.cloud.contract.verifier.messaging.amqp.ContractVerifierAmqpAutoConfiguration.ContractVerifierMessageing(ContractVerifierAmqpAutoConfiguration.java:82)
我刚刚解决了这个问题-。问题在于Mockito迁移(以及我添加的两个缺失测试)。因此,Mockito验证传递的对象是否属于给定类改变了其行为。在以前的版本中,它接受空值,现在不接受空值。缺少验证空值的测试,否则,显然我们会更早发现它。谢谢你的报告,谢谢你的样品。你需要展示你的测试用例。并告诉我们你是哪一个版本的合同using@GaryRussell我已经添加了基本测试,它将生成MessagingTest
将运行mvn clean install
@MarcinGrzejszczak更新,并添加了版本信息。我在之前的经验中使用过Spring Cloud Contract,但没有使用消息传递。请将采样器上载到github好吗?