dom*_*omi 9 spring spring-test spring-rabbit spring-amqp spring-rabbitmq
如何模拟spring rabbitmq/amqp,以便在尝试自动创建交换/队列时在Spring Boot Test期间不会失败?
鉴于我有一个简单的RabbitListener,将导致队列和交换自动创建如下:
@Component
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(value = "myqueue", autoDelete = "true"),
exchange = @Exchange(value = "myexchange", autoDelete = "true", type = "direct"),
key = "mykey")}
)
@RabbitListenerCondition
public class EventHandler {
@RabbitHandler
public void onEvent(Event event) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
在简单的Spring Boot Test中,如下所示:
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = { Application.class })
@Autowired
private ApplicationContext applicationContext;
@Test
public void test() {
assertNotNull(applicationContext);
}
}
Run Code Online (Sandbox Code Playgroud)
它将失败:
16:22:16.527 [SimpleAsyncTaskExecutor-1] ERROR o.s.a.r.l.SimpleMessageListenerContainer - Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:309)
Run Code Online (Sandbox Code Playgroud)
在这个测试中我不关心Rabbit/AMQP,那么如何模拟整个Rabbit/AMQP呢?
Loï*_*yen 10
我知道这是一个古老的话题,但我想介绍一个我正在开发的模拟库:rabbitmq-mock.
这个模拟的目的是模仿没有IO的RabbitMQ行为(没有启动服务器,监听某些端口等)和少量(〜无)启动时间.
它在Maven Central中可用:
<dependency>
<groupId>com.github.fridujo</groupId>
<artifactId>rabbitmq-mock</artifactId>
<version>1.0.10</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
基本用法是使用测试覆盖Spring配置:
@Configuration
@Import(AmqpApplication.class)
class AmqpApplicationTestConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
return new CachingConnectionFactory(MockConnectionFactoryFactory.build());
}
}
Run Code Online (Sandbox Code Playgroud)
要自动模拟Spring bean进行测试,请看一下我正在研究的另一个项目:spring-automocker
希望这可以帮助!
不确定这是否有帮助,但是,我遇到了同样的问题。所以,我只是用@MockBean在RabbitAdmin了不同的配置文件,并没有得到相同的连接问题。测试通过。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@RunWith(SpringRunner.class)
@ActiveProfiles("my-test")
public class ServiceTests {
@Autowired
private DummyService unitUnderTest;
@MockBean
private RabbitAdmin rabbitAdmin;
// lots of tests which do not need Spring to Create a RabbitAdmin Bean
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在我们的项目中,我们在本地RabbitMQ使用docker容器初始化一个实例。RabbitMQ为了运行集成测试,我们在测试用例开始时启动一个实例,并在清理期间将其关闭。
我们正在使用 TestContainers 来做到这一点。请参阅https://www.testcontainers.org/usage/dockerfile.html和/或https://www.testcontainers.org/usage/docker_compose.html。
有点类似于Rajkishan 的答案,但对我不起作用:
这对我有用:
@SpringBootApplication
public class MyTestsApp {
@Bean
@Primary
public CachingConnectionFactory rabbitAdmin() {
return Mockito.mock(CachingConnectionFactory.class);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyTestsApp.class})
@ActiveProfiles(profiles = "test")
public class MyTests {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17058 次 |
| 最近记录: |