如何将属性值注入Spock测试? [英] How to inject property values into a Spock test?
问题描述
当使用Spock测试时,我已经硬编码了spock测试的一些硬编码属性。该示例是一个JDBC URL。我将@Value注释与properties文件一起使用,但这似乎不起作用,因为我的测试没有刻板印象。是否有其他解决方案来注入属性值?
@ContextConfiguration(locations =classpath *:applicationContext-test.xml)
class RepositoryTest extends Specification {
@Shared sql = Sql.newInstance(jdbc:sqlserver:// - room - for - properties)
}
@Shared属性不能被注入,但是应该像这样(使用Spring 3):
@Value(#{databaseProperties.jdbcUrl})
字符串jdbcUrl
sql sql
def setup(){
if(!sql){
sql = new Sql(jdbcUrl)
}
假定你已经在你的bean定义文件中定义了databaseProperties:
< util:properties id =databasePropertieslocation =classpath:database.properties/>
When using a Spock test, i have hardcoded some properties hardcoded into the spock test. The example is a JDBC url. I tried the @Value annotation together with a propertie file, but this seems not to work as my test has no stereotype. Are there any other solutions to inject property values?
@ContextConfiguration(locations = "classpath*:applicationContext-test.xml")
class RepositoryTest extends Specification {
@Shared sql = Sql.newInstance("jdbc:sqlserver:// - room - for - properties")
}
解决方案 @Shared properties cannot be injected, but something like this should work (with Spring 3):
@Value("#{databaseProperties.jdbcUrl}")
String jdbcUrl
Sql sql
def setup() {
if (!sql) {
sql = new Sql(jdbcUrl)
}
}
This assumes that you have defined "databaseProperties" in your bean definition file:
<util:properties id="databaseProperties" location="classpath:database.properties" />
这篇关于如何将属性值注入Spock测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!