使用 Spock 测试时,我将一些属性硬编码到 spock 测试中。该示例是 JDBC url。我尝试将 @Value 注释与属性文件一起使用,但这似乎不起作用,因为我的测试没有构造型。还有其他解决方案来注入(inject)属性值吗?
@ContextConfiguration(locations = "classpath*:applicationContext-test.xml")
class RepositoryTest extends Specification {
@Shared sql = Sql.newInstance("jdbc:sqlserver:// - room - for - properties")
}
最佳答案
要使用PropertySourcesPlaceholderConfigurer
,请添加@Configuration
类:
@Configuration
@PropertySource("classpath:database.properties")
public class DatabaseConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
引用号是here .
然后,在 Spock 中:
@Value('${foo.bar}') //Use single quote.
String fooBar
使用单引号的原因是here .
您可能需要将 @ContextConfiguration
添加到您的 Spock 类中:
@ContextConfiguration(classes = DatabaseConfig .class)
class Test extends Specification {
...
}
关于spring - 如何将属性值注入(inject)到 Spock 测试中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6536289/