在单元测试中为@Import spring 配置设置@Value 属性?

Set an @Value property for an @Import spring config in a unit test?

如何为我 @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?请注意,我只想为这个单元测试使用它,所以在命令行上指定一些东西是不行的。

您可能需要查看 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html#properties--

示例来自 spring docu:

@ContextConfiguration
@TestPropertySource(properties = { "timezone = GMT", "port: 4242" })