简体   繁体   English

单元测试时Spring Boot删除@Component

[英]Spring Boot remove @Component when unit testing

Bear with me as this is the first time I've used Spring Boot so this is only what I think is happening... 请忍受,因为这是我第一次使用Spring Boot,所以这只是我认为正在发生的事情...

I have a couple of methods which are annotated with @Scheduled . 我有几种用@Scheduled注释的方法。 They work great, and I have all of the dependencies configured and injected. 它们工作得很好,我已经配置并注入了所有依赖项。 These dependencies are quite heavy weight, relying on internet connections etc. I've annotated them as @Lazy , so they're only instantiated at the very last minute. 这些依赖项非常繁重,依赖于Internet连接等。我已将它们注释为@Lazy ,因此它们仅在最后一刻实例化。

However, the classes which contain the scheduled methods need to be marked with @Component which means they're created on launch. 但是,包含计划方法的类需要用@Component标记,这意味着它们是在启动时创建的。 This sets off a chain reaction which creates all of my dependencies whether I actually need them for the test I'm currently running. 这引发了连锁反应,无论我实际上是否需要它们来进行当前正在运行的测试,它都会创建我的所有依赖项。

When I run my unit tests on our CI server, they fail because the server isn't auth'd with the database (nor should it be). 当我在CI服务器上运行单元测试时,它们失败,因为该服务器未与数据库进行身份验证(也不应该)。

The tests which test these @Scheduled jobs inject their own mocks, so they work fine. 测试这些@Scheduled作业的测试将注入自己的模拟,因此它们可以正常工作。 However, the tests which are completely unrelated are causing the problems as the classes are still created. 但是,完全不相关的测试会导致问题,因为仍在创建类。 I obviously don't want to create mocks in these tests for completely unrelated classes. 我显然不想在这些测试中为完全不相关的类创建模拟。

How can I prevent certain a @Component from being created when the tests are running? 测试运行时如何防止创建某些@Component

Scheduled jobs class: 预定的作业类别:

package example.scheduledtasks;

@Component
public class ScheduledJob {

    private Database database;

    @Autowired
    public AccountsImporter(Database database) {
        this.database = database;
    }

    @Scheduled(cron="0 0 04 * * *")
    public void run() {
        // Do something with the database
    }
}

Config class: 配置类:

package example

@Configuration
public class ApplicationConfig {

    @Bean
    @Lazy
    public Database database() {
        return ...;// Some heavy operation I don't want to do while testing.
    }

} }

I know you said: 我知道你说:

I obviously don't want to create mocks in these tests for completely unrelated classes. 我显然不想在这些测试中为完全不相关的类创建模拟。

Still, just so you know, you can easily override the unwanted component just for this test: 如此,您仍然可以轻松地覆盖此测试所需的不需要的组件:

@RunWith(...)
@Context...
public class YourTest {
    public static class TestConfiguration {
        @Bean
        @Primary
        public Database unwantedComponent(){
            return Mockito.mock(Database.class);
        }
    }

    @Test
    public void yourTest(){
        ...
    }
}

Similar question/answer: Override a single @Configuration class on every spring boot @Test 相似的问题/答案: 在每个春季启动@Test上覆盖单个@Configuration类

Just add the following to your test class: 只需将以下内容添加到您的测试类中:

@MockBean
public Database d;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM