如何为我 @Import
到单元测试中的上下文设置 @Value
属性?
例如:
@Configuration
public void DatabaseTestsConfiguration
{
@Value("${test.generateddl:true}")
private boolean generateDdl;
@Bean
public JpaVendorAdapter jpaVendorAdapter()
{
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql( false );
hibernateJpaVendorAdapter.setGenerateDdl( generateDdl );
hibernateJpaVendorAdapter.setDatabase( Database.H2 );
return hibernateJpaVendorAdapter;
}
// Some more @Bean's here
}
在我的单元测试中:
@Test
@ContextConfiguration
public void SomeTest extends AbstractTransactionalTestNGSpringContextTests
{
@Configuration
@Import(DatabaseTestsConfiguration.class)
public static class TestConfiguration
{
}
}
现在如何在单元测试中将属性 test. generateddl
重写为 false
?请注意,我只想将其用于此单元测试,因此不能在命令行上指定某些内容。
最佳答案
来自 spring docu 的示例:
@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" })
关于java - 在单元测试中为 @Import spring 配置设置 @Value 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28278217/