How do I import configuration classes in a @DataJpaTest in a SpringBootTest?

后端 未结 3 1805
孤城傲影
孤城傲影 2020-12-31 04:54

I have a SpringBoot Application and I a config package with

@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}
         


        
3条回答
  •  抹茶落季
    2020-12-31 05:18

    You can try this: annotate PersistenceConfig with @ComponentScan to enable component scanning in Spring.

    @Configuration
    @EnableJpaAuditing
    @ComponentScan(basePackages = "com.yourbasepackage")
    public class PersistenceConfig {
    }
    

    With no further configuration, @ComponentScan will default to scanning the same package as the PersistenceConfig class.

    And add the @Context-Configuration annotation to tell it to load its configuration from the PersistenceConfig.class.

    @RunWith( SpringRunner.class )
    @DataJpaTest
    @ContextConfiguration(classes=PersistenceConfig.class)
    public class PersonRepositoryTest {
    
        // Tests ...
    }
    

提交回复
热议问题