@ContextConfiguration
标记。
假设我有一个这样的课程
public ClassUnderTest {
@Autowired
SomeDependency someClass;
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public TestClass {
@Autowired
ClassUnderTest classUnderTest;
}
如果SomeDependency
的自动装配失败,我希望这会失败。顺便说一下,我忘了添加行
我的applicationContext.xml中的<context:annotation-config/>
。也没有<context:component-scan/>
我所期望的是弹簧未通过测试。但它并没有。它快乐地通过,我在部署到容器时发现错误。
为什么会这样?我从文档中看到它使用了一个&#39; SmartContextLoader&#39;也许默认情况下默认启用Annotation支持。但它不应该真的 我错过了什么吗?如何使此测试失败?
答案 0 :(得分:0)
我相信this是解释您看到此行为的相关文档。引用如下:
对于Spring TestContext Framework的所有配置,标准语义支持以下注释。请注意,这些注释并非特定于测试,可以在Spring Framework中的任何位置使用。
* @Autowired
* @Qualifier
* @Resource (javax.annotation) if JSR-250 is present
* @Inject (javax.inject) if JSR-330 is present
* @Named (javax.inject) if JSR-330 is present
* @PersistenceContext (javax.persistence) if JPA is present
* @PersistenceUnit (javax.persistence) if JPA is present
* @Required
* @Transactional
实际上在源代码中,经过挖掘:SpringJUnit4ClassRunner
创建了一个TestContextManager
,后者又定义了TestExecutionListener
的列表。在默认侦听器列表中有一个 - DependencyInjectionTestExecutionListener
- 在injectDependencies
方法中将使用AutowireCapableBeanFactory
自动装配测试实例的属性。相关的代码是here。