Announcement Announcement Module
Collapse
No announcement yet.
Using PropertyPlaceholderConfigurer to test DAO layer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using PropertyPlaceholderConfigurer to test DAO layer

    I would like my applicationcontext to load a different file if I am running my unit tests, versus if I am deploying to a container.

    I would like to use the in memory Hsqldb for quickly running through my tests that hit the database. For actual deployment, I want to use a mysql db.

    Here is what I have so far:
    Code:
    	
    <!--  ======================== Persistence Layer ============================ -->
    	
    	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
    	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="/WEB-INF/jdbc.properties"/>
    	</bean>
    
    	<!-- 
    		Simple local DataSource that works in any environment.
    		This uses the JDBC DriverManager to obtain connections, and does NOT perform connection
    		pooling. Connection pooling is essential to all real-world applications.
    		This definition is good for getting started, as it introduces no dependencies beyond
    		the JDK, but DriverManagerDataSource is not intended for production usage.
    	 -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${jdbc.driverClassName}"/>
    		<property name="url" value="${jdbc.url}"/>
    		<property name="username" value="${jdbc.username}"/>
    		<property name="password" value="${jdbc.password}"/>
    	</bean>
    	
    	<bean id="baseDao" class="com.infoglide.biscuit.dao.BaseDaoImpl" abstract="true">
    		<constructor-arg>
    			<ref bean="dataSource"/>
    		</constructor-arg>
    		<constructor-arg>
    			<ref bean="biscuitManager"/>
    		</constructor-arg>
    	</bean>
    However, this is using the WEB-INF/jdbc.properties file. Right now I will need to go in and manually comment out the mysql db properties and uncomment the hsqldb properties and keep going back and forth with the same process.

    In the style of dependency injection, how do I make this such that when running my tests, I "inject" in a new hsqljdbc.properties with my test db config and all other times I use the jdbc.properties? The manual commenting/uncommenting seems very crude.

  • #2
    The solution I use is to have two PropertyPlaceholderConfigurer definitions in two files - one for production and one for environment (basically only the property files are different). For tests, the test-configuration.xml is used while in production, production-configuration.xml is chosen (for example). This can be done through some sort of basic test class - this way no matter if the tests are fired from the editor or from command line, they still work.

    Comment


    • #3
      Use two properties files?

      Or you could use two jdbc.properties files, one in your web app (/WEB-INF in the war file), and the other in another location (e.g. /WEB-INF in the unit test source directory). You can change the class path for your unit test so that the web app properties file is not on the classpath, but the other jdbc.properties is.

      Comment


      • #4
        Use a system property to indicate which properties file to use

        You can define:
        Code:
        	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
        	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
        	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        		<property name="location" value="${jdbc.properties}"/>
        	</bean>
        And you define a system property called "jdbc.properties". In production, the value of this system property is set to "/WEB-INF/jdbc.properties" while in test environment or JUnit, set the value of this system property to "/WEB-INF/hsqljdbc.properties".

        Comment

        Working...
        X