我遇到了一個特殊的情況。我使用SpringJunit4Runner來運行一些集成測試。我在測試中使用@ContextConfiguration
標籤。即使<context:annotation-config>缺失,Spring @ Autowired也不會失敗
可以說我有這樣的
public ClassUnderTest {
@Autowired
SomeDependency someClass;
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public TestClass {
@Autowired
ClassUnderTest classUnderTest;
}
我想,如果自動裝配SomeDependency
失敗,這FAILFAST類。順便說一下,我忘了在我的applicationContext.xml中添加
<context:annotation-config/>
行。沒有<context:component-scan/>
。
我所期望的是春季測試失敗。但事實並非如此。它通過愉快,我發現在部署到容器過程中的錯誤。
這是爲什麼?我從文檔中看到它使用'SmartContextLoader',也許默認情況下默認啓用Annotation支持。但它不應該。
我錯過了什麼嗎?我怎樣才能使這個測試failfast?
不錯的閱讀[這裏](http://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles/) –
嗯,你不需要對此進行測試,因爲註解'@ Autowired'具有'required'屬性,它默認爲'true',所以如果它沒有自動裝配,你將無法加載應用程序上下文 – Jaiwo99
@ Jaiwo99'required '只有在解釋'@ Autowired'註釋時纔會啓動,因爲我忘記了在applicationcontext.xml中啓用註釋 – Devu