【发布时间】:2016-08-07 02:29:46
【问题描述】:
我有一个项目,我正在为 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 ?似乎有些概念我没有得到。 提前致谢。 非常感谢。
【问题讨论】:
-
您正在硬编码属性文件的路径。而是在您的测试(以及测试之外的配置类)上使用
@PropertySource来访问正确的属性。 -
@dambros :我在 Web.xml 中删除了
location="classpath:fr/insee/config/ddc-dao-test.properties"/> 并添加了@ EnseigneDaoTest 中的 PropertySource({"/ddc-dao-test.properties"})。 ddc-dao-test 未加载,我得到嵌套异常是 java.lang.IllegalStateException:无法加载 JDBC 驱动程序类 [${fr.insee.database.ddc.driverClassName}] -
应该是
@PropertySource("classpath:ddc-dao-test.properties")。 -
问题是
这是加载主应用程序配置,它加载了每个 Java 配置,包括主配置。不需要@PropertyResource。
标签: java spring unit-testing jpa configuration