我有以下代码:
Bean的实现:
package my.persist.services;
@Component
public class MyService{
}
测试:
package my.persist.services;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={"other configuration classes", my.persist.services.MyService.class})
@ComponentScan(basePackageClasses = {my.persist.services.DummyPlaceHolder.class})
public class MyServiceTest extends AbstractJUnit4SpringContextTests {
@Autowired
MyService service;
}
当我从@ContextConfiguration
中删除“ my.persist.services.MyService.class ”时,编译器会说“无法自动装配,没有找到...的bean”,它似乎是{{ 1}}没有效果?有什么帮助吗?
答案 0 :(得分:0)
对基础包的通配符扫描是否有效,而不是对各个类进行组件扫描?
@ComponentScan(basePackages = {"my.persist.services.*"})
您可以在测试中排除某些内容,例如,如果要在测试中过滤掉实际的实现,可以执行以下操作:
@ComponentScan(basePackages = {"my.persist.services.*"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=MyService.class)})