【发布时间】:2011-05-12 01:26:51
【问题描述】:
在运行 junit 测试时,我无法获取应用程序上下文以从外部属性文件加载属性。
鉴于以下情况:
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/app-config.xml")
public class JdbcWatsonDaoTests {
@Autowired
JdbMyDao jdbcMyDao;
@Before
public void setUp() throws Exception {
}
@Test
public void testMethod() {
doSomeStuff();
}
}
app-config.xml
<util:properties id="aProperties" location="classpath:spring/a.properties" />
<util:properties id="bProperties" location="classpath:spring/b.properties" />
<bean id="oracleDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${oracle.url}"/>
<property name="username" value="${oracle.username}"/>
<property name="password" value="${oracle.password}"/>
</bean>
a.properties 和 b.properties 文件与 app-config.xml 位于同一位置...
我发现在运行测试时,属性占位符(文字 "${property}" )是发送到 oracle 服务器的内容,而不是属性文件中的值。
我也尝试过使用 PropertyPlaceholderConfigurer 而不是 bean 配置,但它仍然找不到/包含属性。
我正在使用 eclipse helios、spring 3.0.5、最新版本 m2eclipse 和 4.4 junit。我不得不为不同的 maven/junit 错误降级 junit。
在 tomcat 中发布时,会读取并正确使用属性。我只在运行 junit 测试时才看到问题。
【问题讨论】:
-
您是通过 maven 构建还是通过 eclipse 运行测试 > run as > junit test?
-
eclipse>run as>junit test maven 此时仅管理依赖项。我之所以提出它,是因为我遇到了另一个与构建路径顺序有关的错误。
标签: java eclipse spring junit maven