【发布时间】:2020-07-20 16:15:29
【问题描述】:
我正在尝试通过使用 H2 数据库而不是实际数据库来对 DAO 类进行单元测试。我在尝试让我的测试用例使用 src/main/resources/properties/ 文件夹中存在的属性文件时遇到问题:
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource("classpath:properties/common.properties")
@ContextConfiguration(locations = { "/spring/common-context.xml" })
public class ConfigDAOImplTest {
@Autowired
private ConfigDAOImpl configDAO;
@Spy
private ContextParamDAO contextParamDAO = new ContextParamDAOImpl();
private static final String SCHEMA_CONFIG = "classpath:data/CONFIG_SCHEMA.sql";
private static final String DATA_CONFIG = "classpath:data/CONFIG_DATA.sql";
@Before
public void init() {
MockitoAnnotations.initMocks(this);
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript(SCHEMA_CONFIG)
.addScript(DATA_CONFIG)
.build();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//override the jdbcTemplate for the test case
configDAO.setJdbcTemplate(jdbcTemplate);
configDAO.setContextParamDAO(contextParamDAO);
}
//.. more coode
}
common-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="commonAppProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>file:${conf_folder_path}/common.properties</value>
</list>
</property>
</bean>
<bean id="configDAO"
class="com.myproject.common.dataaccess.impl.ConfigDAOImpl" scope="step">
<property name="jdbcTemplate" ref="jdbcTemplate" />
<property name="corePoolSize" value="${threadpool.size}"/>
</bean>
</beans>
当我运行测试类时,我得到以下异常:
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'corePoolSize'; nested exception is java.lang.NumberFormatException: For input string: "${threadpool.size}"
测试用例无法找到所需属性的一个原因是:
-
PropertyPlaceholderConfigurerbean 指的是{conf_folder_path}/common.properties这是路径src/main/resources/properties/common.properties被 Maven 构建系统复制到。 - 但是,在 Eclipse 中,没有
{conf_folder_path},因为它是由 Maven 创建的。
问题: 假设上述原因是问题的根本原因,考虑到 Spring 上下文中引用的路径与源代码。
【问题讨论】:
-
你应该从 src/main/resources/properties/common.properties 复制文件并粘贴到 src/test/resources/properties/common.properties @Ping
-
@Lemmy 我可以。但随后我将使用实际属性文件的“副本”。这更像是一个集成测试,我想使用“实际”属性文件而不是创建一个副本并使用它。 (编辑,将其复制到测试文件夹并没有解决问题。正如我的问题中所解释的, conf_folder_path 是弹簧上下文正在寻找属性的位置“
-
你试过@TestPropertySource吗?
-
@Lemmy 我没有使用 Spring Boot :(
-
@TestPropertySource不是来自 Spring Boot。它位于 Spring Framework 的核心spring-test工件中。
标签: java spring junit properties spring-test