Quantcast
Channel: Liquibase Forums
Viewing all articles
Browse latest Browse all 1169

Drop-create data after each test with SpringLiquibase

$
0
0

I'm using Springliquibase 3.3.2. I read the datas for db in csv files.

I have unit tests running with SpringLiquibase and I wanted the database returned to it's initial state after each test run.

I'm setting dropFirst == true and was expecting this to restore the hsql db but it does not. I see data changes in one test that were performed in previous tests. My code to define the SpringLiquibase :

@Configuration

@EnableJpaRepositories(basePackages={"my.package"})
@EntityScan(basePackages={"my.package"})
@EnableTransactionManagement
public class DatabaseConfiguration implements EnvironmentAware {
private RelaxedPropertyResolver propertyResolver;
 
private Environment env;
 
private DataSource dataSource;
private SpringLiquibase springLiquibase;
 
@Autowired(required = false)
private MetricRegistry metricRegistry;
 
@Override
public void setEnvironment(Environment env) {
    this.env = env;
    this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
}
 
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingClass(name = "fr.ticks.pp.front.config.HerokuDatabaseConfiguration")
@Profile("!" + "cloud")
public DataSource dataSource() {
    log.debug("Configuring Datasource");
    if (propertyResolver.getProperty("url") == null && propertyResolver.getProperty("databaseName") == null) {
        log.error("Your database connection pool configuration is incorrect! The application" +
                "cannot start. Please check your Spring profile, current profiles are: {}",
                Arrays.toString(env.getActiveProfiles()));
 
        throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(propertyResolver.getProperty("dataSourceClassName"));
    if (propertyResolver.getProperty("url") == null || "".equals(propertyResolver.getProperty("url"))) {
        config.addDataSourceProperty("databaseName", propertyResolver.getProperty("databaseName"));
        config.addDataSourceProperty("serverName", propertyResolver.getProperty("serverName"));
    } else {
        config.addDataSourceProperty("url", propertyResolver.getProperty("url"));
    }
    config.addDataSourceProperty("user", propertyResolver.getProperty("username"));
    config.addDataSourceProperty("password", propertyResolver.getProperty("password"));
 
    if (metricRegistry != null) {
        config.setMetricRegistry(metricRegistry);
    }
 
    dataSource = new HikariDataSource(config);
    return dataSource;
}
 
@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setDataSource(dataSource);
    liquibase.setChangeLog("classpath:config/liquibase/master.xml");
    liquibase.setContexts("development, production");
    // ne lance liquibase que si on utilise H2 en mémoire
    boolean runningH2 = isRunningH2();
    liquibase.setShouldRun(runningH2);
    if (runningH2) {
        log.debug("Configuring Liquibase for H2");
    }
 
    return liquibase;
}

And my test file :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public abstract class AbstractPPTest
{
 
  @Autowired
  protected ApplicationContext applicationContext;
 
  protected MockWebServiceClient mockClient;
 
  @Before
  public void initMock()
  {
    assertNotNull(applicationContext);
    mockClient = MockWebServiceClient.createClient(applicationContext);
    testDate = Calendar.getInstance();
  }
 
@Test
public void test
{
...
}


Is there a non-manual (or manual) way to reset the data between tests in test files ?

Thanks


Viewing all articles
Browse latest Browse all 1169

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>