我有一個集成測試,執行以下操作:自動裝配在Spring TestContext框架
@ContextConfiguration(locations={..path to xmls..})
class IntegTestBase{
--- some field initializations; ---
}
class MyIntegTest extends IntegTestBase{
@Test
public testingMethod(){
MyClass obj = new MyClass();
obj.methodToBeTested();
---- some assertions ----
}
}
class MyClass{
@Autowired
Something obj1;
public int methodToBeTested(){
--- Somecode ---
}
}
在上面的代碼中,我想,當測試用例運行MyClass的對象將創建的所有字段將被自動裝配。但是當測試運行時,所有自動裝配的字段都是空的。它沒有抱怨無法找到bean定義,所以我假設測試上下文在這裏是可見的。但我不明白爲什麼它沒有接線。另一方面,我可以在測試類中創建這些字段,自動裝載它們並將其設置爲創建的對象。有人可以告訴爲什麼這些字段爲空嗎?