简体   繁体   中英

Spring test properties file overriden by main properties file

I have a project where I am setting up Spring test configuration, for the DAO (JPA) layer. My test config gets loaded but only after the main config, especially properties file. So instead of trying to connect to HSQLDB, my test connects to the "real" PostGre database.

Here is my test class :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/spring-dao-test.xml"})
@Transactional
@Rollback
public class EnseigneDaoTest {

    @Autowired
    EnseigneDao enseigneDao;

    public EnseigneDaoTest() {
        // TODO Auto-generated constructor stub
    }

    @Test
    public void testFindById() {
        Enseigne enseigne = enseigneDao.findById(Enseigne.class, "4");

        assertNotNull(enseigne);
        assertEquals("Auchan should have ID 4", "Auchan", enseigne.getLibelle());
    }
}

Here is the spring-dao-test.xml :

<!-- Searches for entities in this package, no need for Persistence.xml -->
<context:component-scan base-package="fr.xxx.ddc.dao" />

<context:property-placeholder
    location="classpath:fr/insee/config/ddc-dao-test.properties"/>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="fr.xxx.ddc.model" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect" ref="jpaDialect" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">${fr.xxx.ddc.hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.max_fetch_depth">${fr.xxx.ddc.hibernate.max_fetch_depth}</prop>
            <prop key="hibernate.default_batch_fetch_size">${fr.xxx.ddc.hibernate.default_batch_fetch_size}</prop>
            <prop key="hibernate.id.new_generator_mappings">true</prop>
        </props>
    </property>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="HSQL"/>
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="true"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${fr.xxx.database.ddc.driverClassName}"/>
    <property name="url" value="${fr.xxx.database.ddc.url}" />
    <property name="username" value="${fr.xxx.database.ddc.username}" />
    <property name="password" value="${fr.xxx.database.ddc.password}" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />

The ddc-dao-test.properties :

fr.xxx.ddc.hibernate.schema=ddc
fr.xxx.ddc.hibernate.max_fetch_depth=3
fr.xxx.ddc.hibernate.default_batch_fetch_size=15
fr.xxx.ddc.hibernate.hbm2ddl.auto=create
fr.xxx.ddc.hibernate.show_sql=false

#Driver H2 pour test
fr.xxx.database.ddc.driverClassName=org.hsqldb.Driver

fr.xxx.database.ddc.url=hsqldb:mem:
fr.xxx.database.ddc.username=sa
fr.xxx.database.ddc.password=

and I get the following log :

09:24:40.262 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding [class path resource [fr/insee/config/ddc-dao.properties]] PropertySource with lowest search precedence
[...]
09:24:41.717 [main] INFO org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from class path resource [fr/insee/config/ddc-dao-test.properties]
09:24:41.718 [main] DEBUG org.springframework.core.env.MutablePropertySources - Adding [localProperties] PropertySource with lowest search precedence
09:24:41.719 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'fr.xxx.ddc.hibernate.max_fetch_depth' in [environmentProperties]
09:24:41.719 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'fr.xxx.ddc.hibernate.max_fetch_depth' in [systemProperties]
09:24:41.720 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'fr.xxx.ddc.hibernate.max_fetch_depth' in [systemEnvironment]
09:24:41.720 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'fr.xxx.ddc.hibernate.max_fetch_depth' in [class path resource [fr/insee/config/ddc-dao.properties]]
09:24:41.720 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'fr.xxx.ddc.hibernate.max_fetch_depth' in [class path resource [fr/insee/config/ddc-dao.properties]] with type [String] and value '3'
09:24:41.721 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'fr.xxx.ddc.hibernate.max_fetch_depth' in [environmentProperties] with type [String] and value '3'

How do I get rid of the ddc-dao.properties ? It seems there are some concepts I didn't get. Thanks in advance. Thanks a lot.

Finally I managed to do it. The problem is indeed the

<context:component-scan base-package="fr.xxx.ddc.dao" />

loading DaoConfiguration.java, itself loading the main .properties file. I needed to exclude the class, which is done this way :

<context:component-scan base-package="fr.xxx.ddc.dao" >
    <context:exclude-filter type="assignable" expression="fr.xxx.ddc.dao.config.DaoConfiguration"/>
</context:component-scan>

and then my DaoConfiguration is no more loaded, nor the ddc-dao-config.xml or the ddc-dao.properties. My in-memory db is used as the log shows :

org.h2.jdbc.JdbcSQLException: Table "ENSEIGNES" not found

Hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM