junit/spring 属性未随应用程序上下文一起加载
运行 junit 测试时,我无法获取应用程序上下文来从外部属性文件加载属性。
给出以下内容:
TestClass
@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 bug 降级 junit。
当在 tomcat 中发布时,会读取并正确使用这些属性。我只在运行 junit 测试时看到问题。
While running a junit test, I cannot get the application context to load properties from external properties files.
Given the following:
TestClass
@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>
and the a.properties and b.properties files are in the same location as app-config.xml...
I've found that when running the test, the properties placeholders (the literal "${property}" )are what is being sent to the oracle server instead of the values in the properties files.
I've also tried using a bean configuration using PropertyPlaceholderConfigurer instead of , but it still doesn't find/include properties.
I am using eclipse helios, spring 3.0.5, newest release m2eclipse and 4.4 junit. I had to downgrade junit for a different maven/junit bug.
When published within tomcat, the properties are read and properly used. I only see the problem when running a junit test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据你的例外情况:
您的问题不是找不到属性,如果找不到属性,异常将类似于
org.springframework.beans.factory.BeanDefinitionStoreException: ...无法解析占位符 'oracle.username'< /code>
这是因为你需要配置一个 PropertyPlaceholderConfigurer 而不是
PropertiesFactoryBean (这就是 util:properties 所做的 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-参考.html#xsd-config-body-schemas-util-properties)
According to your exception:
Your probelm is NOT that the properties are not found, if the properties are not found the exception would be something like
org.springframework.beans.factory.BeanDefinitionStoreException: ... Could not resolve placeholder 'oracle.username'
And this is because you need to configure a PropertyPlaceholderConfigurer instead of a
PropertiesFactoryBean (this is what util:properties does http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#xsd-config-body-schemas-util-properties)
您可以将测试配置文件、spring 上下文、jbdc.properties 分离到 src/test/resources 目录中以尊重 Maven 结构文件。
要配置特殊的 properties 文件进行测试,您必须使用 propertyPlaceHolderConfigurer 在测试 Spring 应用程序上下文中定义它们,如 Ralph 所说。
属性文件必须位于src/test/resources中,并使用斜杠和文件名/a.properties加载它们。将文件放在与 spring 配置文件相同的目录中以加载它。
You may separate your test configuration files, spring context, jbdc.properties, into src/test/resources dir to respect maven structure files.
To configure special properties files for test you have to define them in test spring application context using propertyPlaceHolderConfigurer as Ralph said.
Property files must be in src/test/resources and load them with the slash and file name /a.properties. Place the file in the same directory as the spring configuration file to load it.
看来你正在使用maven。了解您将文件放在哪里会有所帮助。按照惯例,属性文件的测试版本应位于 src/test/resources/ 中,生产版本应位于 src/main/resources 中。它们应该会自动解决。
It appears you're using maven. It would help to know where you put the files. By convention, the test version of the properties files should go in src/test/resources/ and production version in src/main/resources. They should resolve automatically.
我放弃了。我下载了新的 Eclipse 3.6 for Java EE 副本,并通过更新站点方法按照 springsource 工具套件安装说明进行操作。
我将项目导入到具有新工作区的新环境中,一切正常。
我会把它归咎于日食侏儒。
I gave up. I downloaded a fresh copy of Eclipse 3.6 for Java EE and followed the springsource tool suite installation instuctions via the update site method.
I imported my project to the new environment with a new workspace and everythign works just fine.
I'll chalk it up to eclipse gnomes.