vendredi 31 juillet 2020

Spring Boot + Liquibase Test Profile Configuration

I am trying to create a test in-memory h2 database that has schema changes from my liquibase change log. When I run my app, it seems that nothing really happens with the h2 database, and that the liquibase change log is only applied to the main postgresql database in my applications.yml file. I've tried naming the test config file application.yml (in test/resources) and application-test.yml to no success. I've also tried changing the active profile in both application.yml and application-test.yml

application.yml file in project root directory:

spring:
profiles:
    active: test
datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: postgres
    driver-class-name: org.postgresql.Driver
liquibase:
    changelog: classpath:db/changelog/db.changelog-master.xml
jpa:
    hibernate:
        ddl-auto: validate

application-test.yml

spring:
liquibase:
    changelog: classpath:db/changelog/db.changelog-master.xml
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
    user: sa
    password:
    drop-first: true
jpa:
    hibernate:
        ddl-auto: none

Test file:

@RunWith(SpringJUnit4ClassRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
public class SchemaMigrationApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Dependencies:

implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.liquibase:liquibase-core'
testImplementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'
runtimeOnly 'org.postgresql:postgresql'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

Aucun commentaire:

Enregistrer un commentaire