【问题标题】:JUnit Reads applicationContext.xml but claims <context:property-placeholder location="/WEB-INF/jdbc.properties"/> does not existJUnit 读取 applicationContext.xml 但声称 <context:property-placeholder location="/WEB-INF/jdbc.properties"/> 不存在
【发布时间】:2013-06-30 09:47:06
【问题描述】:

我创建了一个 STS Spring MVC 模板应用程序,并希望使用 JUnit 连接并测试我的 Dao。项目在 Tomcat 中加载良好这是我的 servlet 上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />


<!--  JDBC Datasource Configuration Bean  -->
<beans:bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <beans:property name="url" value="${jdbc.url}" />
    <beans:property name="username" value="${jdbc.username}" />
    <beans:property name="password" value="${jdbc.password}" />
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:property-placeholder location="/WEB-INF/jdbc.properties"/>

    <context:component-scan base-package="com.example" />

context:property jdbc.properties 加载正常,直到它被 @ContextConfiguration 读入

这是我的 JUnit 测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
public class ControllerTest{

    @Autowired
    ApplicationContext applicationContext;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private HandlerAdapter handlerAdapter;

    @Autowired
    private HomeController homeController;

    @Before
    public void setUp() {
       request = new MockHttpServletRequest();
       response = new MockHttpServletResponse();

       handlerAdapter = new AnnotationMethodHandlerAdapter();
    }

    @Test
    public void testHallo() throws Exception {

        request.setRequestURI("/");
        request.setMethod("GET");
        final ModelAndView mav = handlerAdapter.handle(request, response,
                   homeController);

       System.out.println("TEST "   + mav.getModelMap());

    }

}

如果我注释掉 jdbc.properties 文件,它可以正常工作

我得到的错误是:

java.lang.IllegalStateException: 无法加载ApplicationContext

原因:org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常为 java.io.FileNotFoundException: 类路径资源 [WEB-INF/jdbc.properties] 无法打开,因为它不存在

项目运行良好,但 JUnit 找不到数据库属性。

这是我尝试过的:

<context:property-placeholder location="file:/WEB-INF/jdbc.properties"/>

还有

但是,这会导致 Tomcat 抛出异常。

【问题讨论】:

    标签: spring junit


    【解决方案1】:

    我认为这是因为在使用 JUnit 和 Servlet 容器时读取路径的方式不同。
    事实上,在使用 Tomcat 时,WEB-INF 文件夹是放在根 Web 应用程序文件夹下的。为此,使用/WEB-INF/jdbc.properties 可以正常工作。
    但是在使用 JUnit 时,情况就不是这样了,因为您将使用 Maven 项目作为目录结构。
    这种情况的一种解决方案是将用于 JUnit 测试的属性文件分开(即使它们与生产属性文件相同)并将它们放在:src/test/resources
    然后在你的 Spring 配置中,使用:

    <context:property-placeholder location="classpath:jdbc.properties"/>
    

    到现在为止,Tomcat 和 JUnit Spring 下都应该可以找到你的文件了

    【讨论】:

    • 同时复制 src/main/resources 文件夹下的上下文文件
    • 我使用&lt;context:property-placeholder location="classpath:jdbc.properties"/&gt; 是否正确,其中jdbc.propertiessrc/main/resources 内部,我可以只做@Value()???
    【解决方案2】:

    在您的 spring applicationContext.xml 文件中,请检查

    <context:component-scan base-package="xxx.controller"/>
    

    component-scan 不应包含具有@Controller 注释的组件

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 2011-11-24
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多