The generated tests(producer side) failed in Spring Cloud contract and AMQP/RabbitMQ
The generated tests(producer side) failed in Spring Cloud contract and AMQP/RabbitMQ
I was trying to use AMQP/RabbitMQ and Spring cloud contract in my microservice projects to define the contract between producers and consumers.
I am using the last Spring Boot 2.0.3, and Spring Cloud Contract 2.0.0.
I have prepared a sample project to reproduce the problem.
In the producer side, I created a base test 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);
}
}
The Sender
just called RabbitTemplate
.
Sender
RabbitTemplate
@Component
public class Sender {
@Autowired
AmqpTemplate amqpTemplate;
public void send(Notification notification){
this.amqpTemplate.convertAndSend("notification.exchange", "notification.messages", notification);
}
}
And created the contract in groovy format.
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")
}
}
}
When I ran mvn clean install
, it failed, and reported the following problem:
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)
And tell us which version of contract you are using
– Marcin Grzejszczak
Jun 29 at 12:13
@GaryRussell I have already added base test, it will generate
MessagingTest
will run mvn clean install
.– Hantsy
Jun 29 at 12:48
MessagingTest
mvn clean install
@MarcinGrzejszczak updated, and added version info. I have used Spring Cloud Contract in before experience, but not used messaging.
– Hantsy
Jun 29 at 12:58
could you please upload a sampler to github?
– Marcin Grzejszczak
Jun 29 at 13:11
1 Answer
1
I've just fixed the issue - https://github.com/spring-cloud/spring-cloud-contract/issues/676 . The problem was with Mockito migration (and 2 missing tests that I've added). So the Mockito verification whether a passed object is of a given class changed its behaviour. In previous versions it accepted nulls, now it doesn't. There were missing tests to verify the null, otherwise, obviously we would have caught it earlier. Thanks for the report, thanks for the sample.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You need to show your test case.
– Gary Russell
Jun 29 at 12:09