Adding configuration class to SpringBootTest breaks component scan

后端 未结 2 1607
既然无缘
既然无缘 2021-01-19 12:47

I am trying to disable real Mongo connection and replace it with Fongo mock in tests.

Here is my test class:



        
相关标签:
2条回答
  • 2021-01-19 12:57

    try use

    • @SpringBootTest @Import(value = TestConfig.class)

    instead of @SpringBootTest(classes = TestConfig.class)

    0 讨论(0)
  • 2021-01-19 13:17

    keep @SpringBootTest and then create a class using @TestConfiguration with the beans in as follows:

    @TestConfiguration
    public class TransactionManagerTestConfiguration {
    
       @Bean
       public String getDatabaseName() {
           return "FongoDB";
       }
    
       @Bean
       public Mongo mongo() {
           return new Fongo(getDatabaseName()).getMongo();
       }
    }
    

    As per the javadoc: Configuration that can be used to define additional beans or customizations for a test. Unlike regular Configuration classes the use of TestConfiguration does not prevent auto-detection of SpringBootConfiguration.

    0 讨论(0)
提交回复
热议问题