Announcement Announcement Module
Collapse
No announcement yet.
PropertyPlaceholderConfigurer JUnit relative resource path Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyPlaceholderConfigurer JUnit relative resource path

    I use a property placeholder configurer in my applicationContext.xml as follows:

    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <description>Configurer that replaces $&#123;...&#125; placeholders with values from properties files</description>
          <property name="location" value="WEB-INF/global.properties"/>
    </bean>
    My web container (tomcat) initializes the application context and sets the web root as a base directory in which to look for relative resources, like the above "WEB-INF/global.properties". It works in Tomcat.

    When I try to run JUnit tests, I want to configure all the same application contexts as in Tomcat - but since I'm using a FileSystemXmlApplicationContext, the base directory for the web application isn't set, and the relative path cannot be resolved:

    Code:
        &#91;junit&#93; Testcase&#58; testActivityInsert&#40;com.lodgingservices.test.data.Test_Acti
    vityDaoImpl&#41;&#58;   Caused an ERROR
        &#91;junit&#93; Could not load properties; nested exception is java.io.FileNotFoundE
    xception&#58; WEB-INF\global.properties &#40;The system cannot find the path specified&#41;
        &#91;junit&#93; org.springframework.beans.factory.BeanInitializationException&#58; Could
     not load properties; nested exception is java.io.FileNotFoundException&#58; WEB-INF
    \global.properties &#40;The system cannot find the path specified&#41;
        &#91;junit&#93; java.io.FileNotFoundException&#58; WEB-INF\global.properties &#40;The system
     cannot find the path specified&#41;
        &#91;junit&#93;     at java.io.FileInputStream.open&#40;Native Method&#41;
        &#91;junit&#93;     at java.io.FileInputStream.<init>&#40;FileInputStream.java&#58;106&#41;
        &#91;junit&#93;     at org.springframework.core.io.FileSystemResource.getInputStream
    &#40;FileSystemResource.java&#58;85&#41;
        &#91;junit&#93;     at org.springframework.core.io.support.PropertiesLoaderSupport.l
    oadProperties&#40;PropertiesLoaderSupport.java&#58;171&#41;
        &#91;junit&#93;     at org.springframework.core.io.support.PropertiesLoaderSupport.m
    ergeProperties&#40;PropertiesLoaderSupport.java&#58;150&#41;
        &#91;junit&#93;     at org.springframework.beans.factory.config.PropertyResourceConf
    igurer.postProcessBeanFactory&#40;PropertyResourceConfigurer.java&#58;69&#41;
        &#91;junit&#93;     at org.springframework.context.support.AbstractApplicationContex
    t.invokeBeanFactoryPostProcessors&#40;AbstractApplicationContext.java&#58;378&#41;
        &#91;junit&#93;     at org.springframework.context.support.AbstractApplicationContex
    t.refresh&#40;AbstractApplicationContext.java&#58;300&#41;
        &#91;junit&#93;     at org.springframework.context.support.FileSystemXmlApplicationC
    ontext.<init>&#40;FileSystemXmlApplicationContext.java&#58;83&#41;
        &#91;junit&#93;     at org.springframework.context.support.FileSystemXmlApplicationC
    ontext.<init>&#40;FileSystemXmlApplicationContext.java&#58;68&#41;
        &#91;junit&#93;     at com.slickapps.spring.test.AbstractTests.loadContextLocations&#40;
    AbstractTests.java&#58;34&#41;
        &#91;junit&#93;     at org.springframework.test.AbstractSpringContextTests.getContex
    t&#40;AbstractSpringContextTests.java&#58;101&#41;
        &#91;junit&#93;     at org.springframework.test.AbstractDependencyInjectionSpringCon
    textTests.setUp&#40;AbstractDependencyInjectionSpringContextTests.java&#58;129&#41;
    How should I initialize all my beans using JUnit? I don't want to have to duplicate all my config files with fully qualified paths for my resources.

    Thanks!

  • #2
    I believe, that the value of the location property should read something like

    Code:
    <property name="location" value="file&#58;c&#58;\....\WEB-INF/global.properties"/>
    You will need to enter the full path to the properties file

    Comment


    • #3
      solution

      For all the properties that I used relative paths (like "WEB-INF/applicationContext.xml"), I changed to absolute paths using a property (like "${homedir}/WEB-INF/applicationContext.xml"). Then I just created a bean that overrides my property configurator and used the full path to my properties file:

      Code:
      <!-- Overrides - This spring beans file is only used by the junit tests to override certain beans
            defined in the normal beans files. -->
         
         <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location"
               value="C&#58;/webapps/Lodging Services/Lodging Services/WEB-INF/global.properties"/>
         </bean>
      FYI - it was also necessary to override my data source bean since my dataSource object was provided by JNDI and I don't have a provider when running Ant with JUnit. I also had to override my tilesConfigurer because Tiles checks to make sure it's running inside a WebApplicationContext (which I'm not). So I added this:

      Code:
      <!-- this bean checks to make sure it's being run inside a WebApplicationContext -->
         <bean id="tilesConfigurer" abstract="true" />

      Comment


      • #4
        Note that if these are unit tests then you shouldn't be using Spring to initialize your beans. Simply create them using the "new" construct and initialize them in the setUp() method.

        Another option is to use a mocking framework such as easyMock to help create instances of the interfaces.

        Comment


        • #5
          Anyone out there that is using a FileSystemResource or ClassPathResource should change your junit code to use ClassPathXmlApplicationContext or FileSystemXmlApplicationContext as the first two resource classes don't handle the PropertyPlaceholderConfigurer tags.

          Comment

          Working...
          X