试图为单元测试目的制作模拟的 Spring Context。我们的 Spring 配置非常大,至于现在尝试使用 Spring + Springockito Annotations 来获得这个东西。
我偶然发现的问题是我希望有多个 Java 类来处理上下文创建/模拟。就目前而言,这看起来像这样(假设 A 类将 B 类和 ClassC 列表作为构造函数参数):
//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;
}
...
配置器.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
,这正是我想要避免的.
将此上下文配置拆分为多个文件是否有更改?