简体   繁体   English

Spring Boot Integration 测试模拟外部依赖

[英]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.

相关问题 Spring 引导集成测试:模拟环境接口 - Spring Boot Integration Test : Mock Environment Interface Spring Boot集成测试通过Autowire注入模拟对象 - Spring boot integration test injecting mock object via autowire Spring 启动+集成测试 - Spring Boot + Integration Test 春季测试模拟一个依赖 - Spring Test mock one dependency Elasticsearch Spring启动集成测试 - Elasticsearch Spring boot integration test 使用Spring Boot和Spock进行集成测试 - Integration Test with Spring Boot and Spock 具有多个参数的 Spring Boot 集成测试模拟 bean 方法返回 null - Spring boot integration test mock bean method with multiple arguments returns null 我们如何在 Spring Boot 中模拟测试 IntegrationFlow。 我在使用 Spring Integration File 的 Spring Boot 应用程序中使用 Spring Integration - How can we mock test IntegrationFlow in spring boot. I am using Spring Integration inside a Spring boot app using Spring Integration File Spring Boot 2.6 + 集成 - internalPublisherAnnotationBeanPostProcessor 循环依赖 - Spring Boot 2.6 + Integration - internalPublisherAnnotationBeanPostProcessor circular dependency 集成测试中 Spring 控制器中的模拟 Feign 客户端 - Mock Feign client in Spring controller in integration test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM