試圖爲單元測試製作嘲諷的Spring上下文。我們的Spring配置是相當大的,至於現在試圖使用Spring + Springockito註解來獲得這個東西。Java Spring:在多個文件中嘲弄上下文
我偶然發現的問題是,我想要有多個Java類來處理上下文創建/模擬。至於現在這看起來是這樣的(假設甲級採取ClassCs的B類和List構造函數的參數):
//declaring context classes below
@ContextConfiguration(loader = SpringockitoAnnotatedContextLoader.class,
classes = {
ClassA.class,
ClassB.class,
SubClassCOne.class,
SubClassCTwo.class,
... //list goes on and on with more mocks
}
)
public class Configurator {
@Autowired
ClassA classA;
@ReplaceWithMock
ClassB classB;
@Autowired
List<ClassC> classesC;
@Autowired
SubClassCOne subclassCOne;
....
問題是C的子類的那個名單是遠遠大於我想在這裏(何況我把上只有一個班全部責任),所以我想另一類是會照顧創作是這樣的:
ListProvider.java:
//THIS ANNOTATION WON'T BE TAKEN INTO ACCOUNT
@ContextConfiguration(loader = SpringockitoAnnotatedContextLoader.class,
classes = {
SubClassCOne.class,
SubClassCTwo.class,
...
}
)
public class ListProvider {
@Autowired
List<ClassC> classesC;
...
public List<ClassC> getClassesC(){
return classesC;
}
...
Configurator.java:
@ContextConfiguration(loader = SpringockitoAnnotatedContextLoader.class,
classes = {
ClassA.class,
ClassB.class,
ListProvider.class,
...
}
)
public class Configurator {
@Autowired
ClassA classA;
@ReplaceWithMock
ClassB classB;
@Autowired
ListProvider listProvider
@Autowired //probably not autowired anymore?
List<ClassC> classesC;
...
//then somewhere it'll take classesC from listProvider using getter?
但隨後問題就來了,即使ListProvider將被正確自動裝配,它不會利用@ContextConfiguration
類的,它不會找到任何ClassC的子類,除非我不會Configurator.java
提供的一切,這正是我想避免的。
是否有將此上下文配置拆分爲多個文件的更改?