java Spring JUnit ApplicationContextInitializer参数
我们已经实施了一项扩展计划。属性格式。这样的属性文件可以包含可选的include属性。此属性的值是要递归加载的其他属性文件的类路径
我可以为我的应用程序配置Spring环境,但我在使用SpringJUnit4ClassRunner
这个机制时遇到了一个问题
我以为我可以使用ContextConfiguration
注释的initializer
属性,但看起来它只能用无参数构造函数实例化
我需要给它我的属性文件层次结构的根文件。它最终可能成为我测试类上的另一个注释,但同样,我如何访问它呢
到目前为止,我唯一的想法是在测试类静态初始值设定项中将该文件设置为系统属性。丑?:
@ActiveProfiles("qacs.controller.channels=mock")
@ContextConfiguration(initializer=ContainerTestContextInitializer.class)
public class QacsControllerTest
{
static
{
System.setProperty(ContainerTestContextInitializer.SYSTEM_PROPERTY, "classpath:com/xxx/qacs/QacsControllerTest.properties");
}
@Test
void test() {}
}
}
public class ContainerTestContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
public static final String SYSTEM_PROPERTY = "icomp.test.properties";
@Override
public void initialize(ConfigurableApplicationContext pApplicationContext)
{
String path = System.getProperty(SYSTEM_PROPERTY);
if (path == null)
{
throw new IllegalStateException("Missing system property " + SYSTEM_PROPERTY);
}
final DefaultPropertiesLoader loader;
loader = new DefaultPropertiesLoader(System.getProperties());
try
{
loader.load(path);
}
catch (IOException e)
{
throw new IllegalStateException(e.getMessage(), e);
}
MutablePropertySources sources = pApplicationContext.getEnvironment().getPropertySources();
MapPropertySource mps = new MapPropertySource(Launcher.ICOMP_PROPERTY_SOURCE, (Map) loader.getProperties());
sources.addFirst(mps);
}
}
共 (0) 个答案