spring-cloud - Spring Cloud Contract for AMQP - RabbitTemplate 的自动装配问题
问题描述
我正在尝试使用 Spring Cloud Contract 为 Spring AMQP 运行合同测试。但是,我遇到了 RabbitTemplate 自动装配的问题。在下面我的 Base Test 类中,自动连接的 RabbitTemplate 需要一个具有有效连接详细信息(RabbitMQ 代理的主机和端口)的 ConnectionFactory。由于合约测试预计不会实际连接到消息代理,因此在测试环境中没有提供给连接工厂的主机和端口。
我得到错误
org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: ${queue.hosts}: nodename nor servname provided, or not known
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:71)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:476)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:614)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:240)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1810)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1784)
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:864)
RabbitTemplate 应该被嘲笑吗?我试过了,但即使这样也行不通。还尝试将模拟 ConnectionFactory 传递给实际的 RabbitTemplate 但它试图从模拟工厂获得真正的连接。您如何解决它试图建立实际连接的问题?
基础测试类
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = QueueConfiguration.class)
@AutoConfigureMessageVerifier
public class CreateNotificationBase {
private String message;
@Autowired
private RabbitTemplate rabbitTemplate;
@Before
public void setUp() {
client = new ChangeNotificationClient();
message = client.buildChangeNotificationMessage();
}
protected void onUserCreation() {
rabbitTemplate.send("change_notification_exchange",
RoutingKey.CHANGE_NOTIFICATION_KEY.getName(),
org.springframework.amqp.core.MessageBuilder.withBody(message.getBytes()).build());
}
}
合同定义
label: user_create
input:
triggeredBy: onUserCreation()
outputMessage:
sentTo: change_notification_exchange
body: ''' {"data":["....."jsonapi":{"version":"1.0"}} '''
自动生成的测试
public class CreateTest extends CreateNotificationBase {
@Inject ContractVerifierMessaging contractVerifierMessaging;
@Inject ContractVerifierObjectMapper contractVerifierObjectMapper;
@Test
public void validate_create() throws Exception {
// when:
onUserCreation();
// then:
ContractVerifierMessage response = contractVerifierMessaging.receive("change_notification_exchange");
assertThat(response).isNotNull();
// and:
Object responseBody = (contractVerifierObjectMapper.writeValueAsString(response.getPayload()));
// assertions
;
}
}
解决方案
推荐阅读
- javascript - Webpack Puppeteer Chromium 安装:无法启动浏览器
- android - 如何让下载的数据不延迟出现在屏幕上?
- java - 使用正则表达式进行字符串操作
- python - python pyodbc SQLite sql注入
- php - 如何在一个数组中使用array_intersect
- c# - WPF MVVM 键绑定
- python-3.x - 如何修补数组中的特定子文档
- java - 在两个会话中在主从 ADF 中详细添加记录
- android - 无法真正通过命令行成功安装 apk
- react-native - BottomTabNavigation:元素类型无效:应为字符串或类/函数,但得到:对象