【发布时间】:2019-05-15 19:30:19
【问题描述】:
根据25.3.3. Excluding Test Configuration这个能力存在:
当放置在顶级类上时,@TestConfiguration 表示 src/test/java 中的类不应该被扫描捡起
我在项目的根路径/包src/test/java/com/example/mvctries/ 中创建了@Configuration FilledWithBeanTestConfig 和@TestConfiguration DummyTestConfig。我的main 包含方法的类故意用@ComponentScan 注释只是为了不小心选择FilledWithBeanTestConfig;关于这种情况Spring特别警告:
如果您的应用程序使用组件扫描(例如,如果您使用 @SpringBootApplication 或 @ComponentScan),你可能会发现顶层 您仅为特定测试创建的配置类 不小心到处被捡起来。
我在@WebMvcTest 带注释的测试类中使用了@Import(DummyTestConfig.class),放在src/test/java/com/example/mvctries/controller/ 中;我原以为 @Configuration FilledWithBeanTestConfig 会被忽略,但事实并非如此。将DummyTestConfig 移动到src/test/java/com/example/mvctries/controller/ 也没有帮助。
在测试上述src/test/java 扫描忽略功能时,我做错了什么?
我应该如何正确使用@TestConfiguration 才能使用src/test/java 扫描忽略功能?
我将如何通过仅使用@SpringBootApplication 组件扫描而不是有意 使用@ComponentScan 扫描来测试此功能?
虽然上述问题可能看起来太多,但实际上是提出以下一个问题的另一种方式:如何制作一个项目以清楚地证明@TestConfiguration 的使用如何导致src/test/java 中的类不应该通过扫描来获取。
更新
这是示例项目:https://github.com/adrhc/spring-boot-test-checks.git;运行应用程序时使用端口 8083;更好地运行测试类。
【问题讨论】:
-
你能分享一个示例项目吗?并明确提及您要实现的目标
标签: spring-boot-test