ctwomey
ctwomey

Reputation: 479

Spring-boot not respecting liquibase properties

I'm in the process of setting up liquibase to manage my database in a new spring boot application. I need the liquibase dependency in my classpath to reset the database state after certain integration tests run. During my tests I do not want liquibase to be enabled via spring auto config during application context initialization. I've tried adding liquibase.enabled = false to the application.properties, however when I debug the LiquibaseAutoConfiguration class it appears that enabled is always set to true.

I'm not new to spring, but I am new to spring-boot's auto configuration. Has anyone had issues with spring boot not respecting properties in application.properties?

My setup is fairly minimal:

Relevant code snippets:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringBootClass.class })
public class databaseTests{
    @Before
    public void setup() throws LiquibaseException, SQLException {
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dataSource.getConnection()));
        Liquibase liquibase = new Liquibase("db/changelog/db.changelog-master.yaml", new FileSystemResourceAccessor("src/main/resources/"),database );
        liquibase.dropAll();
        liquibase.update("test");
    }
..
}

@SpringBootApplication
@Import({ DataSourceConfig.class, HibernateConfig.class, OauthConfig.class })
@EnableConfigurationProperties 
public class SpringBootClass {
..
}

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
<!--   <liquibase.version>3.3.5</liquibase.version> -->


<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <scope>test</scope>
</dependency>

Upvotes: 1

Views: 3064

Answers (2)

ctwomey
ctwomey

Reputation: 479

Should have RTFM...

from spring boot documentation

ConfigFileApplicationContextInitializer is an ApplicationContextInitializer that can apply to your tests to load Spring Boot application.properties files. You can use this when you don’t need the full features provided by @SpringApplicationConfiguration.

@ContextConfiguration(classes = Config.class, initializers = ConfigFileApplicationContextInitializer.class)

Changing my config to use the initializer worked.

Upvotes: 0

Andy Wilkinson
Andy Wilkinson

Reputation: 116321

If you want your tests to consume application.properties you need to run them as a Spring Boot application. Your use of @ContextConfiguration means that you're currently running them as a vanilla Spring Framework application. Replace the @ContextConfiguration annotation with @SpringApplicationConfiguration.

Upvotes: 4

Related Questions