Java ContextConfiguration与WebAppConfiguration注释一起运行
我有一个类对这些注释进行了注释:Java ContextConfiguration与WebAppConfiguration注释一起运行,java,spring,testing,spring-mvc,Java,Spring,Testing,Spring Mvc,我有一个类对这些注释进行了注释: @ContextConfiguration(locations = { "classpath:pathToXml.xml" }) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration 你能解释一下在我的类中添加这些注释后我拥有哪些功能吗?你将获得@WebAppConfigurationconfiguration javadoc中描述的功能 WebApplicationC
@ContextConfiguration(locations = { "classpath:pathToXml.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
你能解释一下在我的类中添加这些注释后我拥有哪些功能吗?你将获得
@WebAppConfiguration
configuration javadoc中描述的功能
WebApplicationContext
主要改变了资源的加载方式,即带有未指定资源前缀的资源将从src/main/webapp
或从value
参数中的位置加载(否则它们在Spring上下文中不可用,因为通常webapp文件夹不包括在类路径中)而不是类路径:
此外,您还可以测试使用其他WebApplicationContext特性(bean、会话和请求bean范围等)的代码
这意味着您将能够使用因此,在使用
@WebAppConfiguration
注释的测试中,您需要从src/test/resources
读取内存中数据库的数据库创建脚本,我注意到在创建的模拟应用程序上下文中,resourceLoader
字段为空。因此,我创建了一个自定义的ApplicationContextInitializer
,它将该字段设置为AnnotationConfigApplicationContext
实例
然后,所有需要访问常规资源的测试只需添加类似@ContextConfiguration(初始值设定项={MyConfigurableContext.class})
看起来很有意思,但很明显