[英]Spring Boot Integration test mock external dependency
I'm trying to create integration tests for my Spring Boot app.我正在尝试为我的 Spring Boot 应用程序创建集成测试。 The idea is to launch an embedded postgres db and run http calls with TestRestTemplate to my controllers.这个想法是启动一个嵌入式 postgres 数据库,并使用 TestRestTemplate 对我的控制器运行 http 调用。
The problem is my project has a dependency we use for redis queues.问题是我的项目具有我们用于 redis 队列的依赖项。
<dependency>
<groupId>com.github.sonus21</groupId>
<artifactId>rqueue-spring-boot-starter</artifactId>
<version>2.9.0-RELEASE</version>
</dependency>
I've tried to mock out the dependencies and most of them work, but with this one it complains I guess because it is a @Configuration
not a @Component
:我试图模拟出依赖项,并且它们中的大多数都可以工作,但是对于这个它抱怨我猜是因为它是@Configuration
而不是@Component
:
Dependency config class:依赖配置 class:
@Configuration
@AutoConfigureAfter({RedisAutoConfiguration.class})
@ComponentScan({"com.github.sonus21.rqueue.web", "com.github.sonus21.rqueue.dao"})
public class RqueueListenerAutoConfig extends RqueueListenerBaseConfig {
public RqueueListenerAutoConfig() {
}
...
}
My test config class我的测试配置 class
@TestConfiguration
public class TestRestTemplateConfig {
@Bean
@Primary
@Order(Ordered.HIGHEST_PRECEDENCE)
public RqueueListenerAutoConfig rqueueListenerAutoConfig() {
return Mockito.mock(RqueueListenerAutoConfig.class);
}
....
}
I've tried with @AutoConfigureOrder(1)
at my config class but the original RqueueListenerAutoConfig
launches before anything and my mocked beans haven't been declared yet.我已经在我的配置 class 中尝试使用@AutoConfigureOrder(1)
,但是原始的RqueueListenerAutoConfig
在任何事情之前启动并且我的模拟 bean 还没有被声明。
To be honest mocking every service on that dependency is a pain, but I haven't figured out a way to mock the whole dependency with a single configuration.老实说 mocking 该依赖项上的每个服务都很痛苦,但我还没有找到一种方法来使用单个配置来模拟整个依赖项。 I tried not loading the dependency when I'm on the test profile but since it runs spring context my code needs it.当我在测试配置文件上时,我尝试不加载依赖项,但由于它运行 spring 上下文,我的代码需要它。
My test class has the following config:我的测试 class 具有以下配置:
@SpringBootTest
@Import(TestRestTemplateConfig.class)
@ActiveProfiles("test")
public class TestClass {
...
}
Any clues?有什么线索吗?
Thanks.谢谢。
Try尝试
@EnableAutoConfiguration(exclude=RqueueListenerAutoConfig.class)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.