具有资源值的SpringJUnit4ClassRunner上下文配置

时间:2017-05-16 21:00:01

标签: java spring junit-runner spring-boot-test

我正在尝试配置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'.

0 个答案:

没有答案