Note that there are some explanatory texts on larger screens.

plurals
  1. POJUnit Reads applicationContext.xml but claims <context:property-placeholder location="/WEB-INF/jdbc.properties"/> does not exist
    primarykey
    data
    text
    <p>I created an STS Spring MVC Template application and wanted to wire in and test my Dao's using JUnit. The project loads fine in Tomcat Here is my servlet context:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt; &lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;annotation-driven /&gt; &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt; &lt;resources mapping="/resources/**" location="/resources/" /&gt; &lt;!-- JDBC Datasource Configuration Bean --&gt; &lt;beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;beans:property name="driverClassName" value="${jdbc.driverClassName}" /&gt; &lt;beans:property name="url" value="${jdbc.url}" /&gt; &lt;beans:property name="username" value="${jdbc.username}" /&gt; &lt;beans:property name="password" value="${jdbc.password}" /&gt; &lt;/beans:bean&gt; &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt; &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt; &lt;beans:property name="suffix" value=".jsp" /&gt; &lt;/beans:bean&gt; &lt;context:property-placeholder location="/WEB-INF/jdbc.properties"/&gt; &lt;context:component-scan base-package="com.example" /&gt; </code></pre> <p></p> <p>The context:property jdbc.properties loads fine until it gets read in by the @ContextConfiguration</p> <p>Here is my JUnit test:</p> <pre><code>@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()); } } </code></pre> <p><b>If I comment out the jdbc.properties file it works fine</b></p> <p>The error I am getting is:</p> <p>java.lang.IllegalStateException: <b>Failed to load ApplicationContext</b></p> <p>Caused by: org.springframework.beans.factory.BeanInitializationException: <b>Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/jdbc.properties] cannot be opened because it does not exist</b></p> <p>The project runs fine but JUnit cannot find the database properties .</p> <p><b>Here is what I have tried:</b></p> <pre><code>&lt;context:property-placeholder location="file:/WEB-INF/jdbc.properties"/&gt; </code></pre> <p>Also </p> <p></p> <p>However, that causes Tomcat to throw an Exception. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload