简体   繁体   中英

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

How do I set an @Value property for a context which I @Import into my unit test?

For example:

@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
}

In my unit test:

@Test
@ContextConfiguration
public void SomeTest extends AbstractTransactionalTestNGSpringContextTests
{

  @Configuration
  @Import(DatabaseTestsConfiguration.class)
  public static class TestConfiguration
  {
  }
}

How do I now override the property test.generateddl to be false in my unit test? Note that I want to have it only for this unit test, so specifying something on the command line is a no-go.

You might want to check http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html#properties--

Example from spring docu :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM