java - Spring 测试属性文件被主属性文件覆盖

标签 java spring unit-testing jpa configuration

我有一个项目,正在为 DAO (JPA) 层设置 Spring 测试配置。我的测试配置被加载,但仅在主配置之后加载,尤其是属性文件。因此,我的测试不是尝试连接到 HSQLDB,而是连接到“真正的”PostGre 数据库。

这是我的测试类:

@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());
    }
}

这是 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 />

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=

我得到以下日志:

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'

如何摆脱 ddc-dao.properties ?看来有些概念我没明白。 提前致谢。 非常感谢。

最佳答案

终于我成功了。 问题确实是

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

加载 DaoConfiguration.java,本身加载主 .properties 文件。 我需要排除该类,这是通过以下方式完成的:

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

然后我的 DaoConfiguration 不再加载,ddc-dao-config.xml 或 ddc-dao.properties 也不再加载。我的内存数据库的使用如日志所示:

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

希望这对某人有帮助。

关于java - Spring 测试属性文件被主属性文件覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36641682/

相关文章:

java - 抛出异常是否涉及新的不同线程?

java - 使用 Spring-MVC 降低 Java Web 应用程序中的部署频率

java - Spring Config 属性未在某个服务器上填充

azure - 如何获取Azure DevOps Pipelines中变量的单元测试结果?

Java文件上传非常慢

java - 将图像插入 Excel - Java

java - Map.put 和 Map.putAll 方法之间的区别?

Spring JUnit 测试 : Autowiring and Transaction Problems

asp.net-mvc - 我如何对这个业务逻辑进行单元测试?

python - 当 SQLAlchemy 事件触发 Celery 任务时连接关闭