我正在尝试配置Spring,Junit和Hibernate(为我们的测试用例启用@Autowire
注释)。我目前的设置是:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
"file:src/main/webapp/WEB-INF/applicationContext.xml",
"file:src/main/webapp/WEB-INF/dispatcher-servlet.xml",
})
public class UserManagementServiceTest {...}
但是,初始化失败并带有
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
我们在context.xml
文件中定义了各种凭据,这些凭据在运行应用程序时由tomcat选取。结构如下
<Context>
<Resource name="abc/abcManager"
auth="Container"
type="path.to.file"
factory="org.apache.naming.factory.BeanFactory"
attr1="asas"
attr2="asda"/>
</Context>
然后使用InitialContext
类来获取这些属性。
如何为我们的测试用例配置上述功能?
将context.xml
文件添加到ContextConfiguration
会出错
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 10; cvc-elt.1: Cannot find the declaration of element 'Context'.