有一个mongo dao类com.foo.bar.dao.CompanyRecommendationDao
,并且有一个相应的测试类:com.foo.bar.dao.CompanyRecommendationDaoTest
。
@RunWith(SpringRunner.class)
@DataMongoTest(includeFilters = @ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE,value={CompanyRecommendationDao.class}))
@Import(SpringMongoConfig.class)
public class CompanyRecommendationDaoTest
第一次运行测试类是可以的,但是如果在Application类上方添加ComponentScan
注释
@SpringBootApplication
@ComponentScan("com.foo")
public class BarApplication
在这种情况下,运行 CompanyRecommendationDaoTest 时可能会出现以下错误
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'companyManagementService': Unsatisfied dependency expressed through field 'companyMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'companyMapper' defined in file [/Users/zhugw/develop/workspace/bar/target/classes/com/foo/bar/mapper/CompanyMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
那么为什么它可以加载其他服务类,它应该只加载 mongo 相关类吗?
来自DataMongoTest
javadoc
Can be used when a test focuses only on MongoDB components. Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MongoDB tests.
显式添加 @ComponentScan("com.foo") 有什么区别(默认包是 com.foo.bar)?
PS。 何时启用跟踪日志情况1(没有@ComponnetScan)
2019-06-02 22:28:08.876 TRACE 13875 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Scanning file [/Users/zhugw/develop/workspace/foo/bar/target/classes/com/foo/bar/dao/CompanyRecommendationDao.class]
2019-06-02 22:28:08.877 TRACE 13875 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.test.context.filter.TestTypeExcludeFilter'
2019-06-02 22:28:08.877 TRACE 13875 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters'
2019-06-02 22:28:08.877 DEBUG 13875 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/Users/zhugw/develop/workspace/foo/bar/target/classes/com/foo/bar/dao/CompanyRecommendationDao.class]
情况2(使用@ComponnetScan)
2019-06-02 22:40:23.989 TRACE 14573 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Scanning file [/Users/zhugw/develop/workspace/foo/bar/target/classes/com/foo/bar/dao/CompanyRecommendationDao.class]
2019-06-02 22:40:23.989 DEBUG 14573 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/Users/zhugw/develop/workspace/foo/bar/target/classes/com/foo/bar/dao/CompanyRecommendationDao.class]
最佳答案
您遇到的问题可能是由于 @ComponentScan
。最初当您使用@DataMongotTest
时仅注释 spring 配置仅运行 mongo 所需的 bean。但是当您将组件扫描注释添加到主类并设置包扫描级别如 "com.foo"
spring会自动在该路径下的所有包中搜索并找到注册的bean。
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
由于您在运行 test 时本质上并未提供包括属性值等在内的整个应用程序数据,因此 spring 无法为 sqlSessionTemplate.java
创建 bean ,这可能需要一些未在测试中加载的属性值。
Annotation that can be used in combination with @RunWith(SpringRunner.class) for a typical MongoDB test. Can be used when a test focuses only on MongoDB components. Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MongoDB tests. By default, tests annotated with @DataMongoTest will use an embedded in-memory MongoDB process (if available).
最好为运行测试定义一个单独的主类,以便您可以在运行测试时完全控制配置。
引用here
关于spring-boot - 为什么 @ComponentScan 然后 DataMongoTest 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56416270/