Wim Deblauwe
Wim Deblauwe

Reputation: 26858

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.

Upvotes: 0

Views: 1013

Answers (1)

sodik
sodik

Reputation: 4683

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" })

Upvotes: 2

Related Questions