在this spring boot project中,集成测试会创建一个spring应用程序上下文,该上下文应该创建所有必需的bean,除了那些带有@ExcludeFromTests
注释的bean
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes=IntegrationTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest_CartController
{
/*....*/
}
测试应用加载@Configuration
类
@Profile("test")
@Configuration
@ComponentScan(basePackages = {"com.example.demo", "com.example.demo.*"},
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
value = ExcludeFromTests.class))
@EnableJpaRepositories("com.example.demo.dao")
@EntityScan("com.example.demo.resource")
@EnableAutoConfiguration
public class TestDemoAppConfig
{
private static final Logger LOG = LoggerFactory.getLogger(TestDemoAppConfig.class);
public TestDemoAppConfig()
{
// I can see this being printed out
LOG.info("_________________________________");
LOG.info("Instantiating TestDemoAppConfig");
LOG.info("_________________________________");
}
}
也就是说,它会在com.example.demo
个包中加载bean,但应该忽略那些用@ExcludeFromTests
注释的bean,例如
@Component
@ExcludeFromTests
public class ItemDaoInit
{
private static final Logger LOG = LoggerFactory.getLogger(ItemDaoInit.class);
public ItemDaoInit(@Autowired ItemDao dao)
{
init(dao);
}
private void init(ItemDao dao)
{
LOG.info(" ------------ item dao preprocessor called"); // I shouldn't see this when I run a test as this bean shouldnt be created
/* preprocess entries in dao */
}
}
当我运行应用程序时,我可以看到加载了正确的@Configuration
但仍然创建了ItemDaoInit
bean
""2018-02-28 11:54:28 [main] INFO com.example.demo.dao.ItemDaoInit - ------------ item dao preprocessor called
为什么创建这个bean虽然它带有我明显从组件扫描中排除的注释?
修改
顺便说一句,如果我从上面的测试配置类中排除@EnableAutoConfiguration
,问题仍然存在
编辑2
这是IntegrationTestApplication
。它只扫描@Configuration
类
@SpringBootApplication
@ComponentScan(basePackageClasses= {TestDemoAppConfig.class})
public class IntegrationTestApplication
{
public static void main(String[] args) {
SpringApplication.run(IntegrationTestApplication.class, args);
}
}
感谢您的帮助