Spring Cloud Contract MessageVerifier 中的 ClassCastException 使用 ActiveMQ

ClassCastException in Spring Cloud Contract MessageVerifier using ActiveMQ

我整个星期都在研究 Spring Cloud Contract。我能够使用 Spring Cloud Stream 来创建生产者和消费者应用程序来进行消息传递。

我正在尝试修改生产者端以使用 ActiveMQ(带有虚拟主题)。我想我快到了。我已经编写了自己的 MessageVerifier 并将其注入到测试的基础 class 中。现在我得到:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.535 sec <<< FAILURE! - in org.springframework.cloud.contract.verifier.tests.someconsumer.MessagingTest validate_inviteContract(org.springframework.cloud.contract.verifier.tests.someconsumer.MessagingTest) Time elapsed: 0.802 sec <<< ERROR! java.lang.ClassCastException: org.apache.activemq.command.ActiveMQTextMessage cannot be cast to org.springframework.messaging.Message at org.springframework.cloud.contract.verifier.messaging.stream.ContractVerifierHelper.convert(ContractVerifierStreamAutoConfiguration.java:59) at org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessaging.receive(ContractVerifierMessaging.java:40) at org.springframework.cloud.contract.verifier.tests.someconsumer.MessagingTest.validate_inviteContract(MessagingTest.java:27)

我可以从日志文件中得知正在发送测试消息,并且我的自定义 MessageVerifier 正在接收消息。我缺少什么步骤?

如果我这样定义 MessageVerifier:

public class ConsumerMessageVerifier implements MessageVerifier< ActiveMQTextMessage>

然后解决了 ClassCastException,但会导致其他问题 'Application Failed to Start' 错误:

Description: Parameter 0 of method contractVerifierMessaging in org.springframework.cloud.contract.verifier.messaging.integration.ContractVerifierIntegrationConfiguration required a bean of type 'org.springframework.cloud.contract.verifier.messaging.MessageVerifier' that could not be found. - Bean method 'contractVerifierMessageExchange' not loaded because @ConditionalOnClass did not find required class 'org.springframework.amqp.rabbit.core.RabbitTemplate' - Bean method 'contractVerifierMessageExchange' not loaded because @ConditionalOnClass did not find required class 'org.apache.camel.Message' - Bean method 'contractVerifierMessageExchange' in 'ContractVerifierIntegrationConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.cloud.contract.verifier.messaging.MessageVerifier; SearchStrategy: all) found bean 'consumerMessageVerifier' - Bean method 'contractVerifierMessageExchange' in 'NoOpContractVerifierAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.cloud.contract.verifier.messaging.MessageVerifier; SearchStrategy: all) found bean 'consumerMessageVerifier' - Bean method 'contractVerifierMessageExchange' not loaded because @ConditionalOnProperty (stubrunner.stream.enabled=true) found different value in property 'stubrunner.stream.enabled'

所以,我不确定那是否是正确的道路。所以我的问题是:

  1. 我应该像上面那样限定 MessageVerifier 吗?

  2. 如果是,解决 'Application Failed to Start' 错误需要哪些步骤?

谢谢!

MessageVerifier 需要输入。您还需要 ContractVerifierMessaging 。您可以查看 Spring 云合同包:org.springframework.cloud.contract.verifier.messaging.amqporg.springframework.cloud.contract.verifier.messaging.stream‌​org.springframework.cloud.contract.verifier.messaging.camelorg.springframework.cloud.contract.verifier.messaging.integr‌​ation 示例。