How to remove h2db in memory between Spring integration tests?

I am using Liquibase in my Spring web application. I have a group of objects with hundreds of tests for the REST API in integration tests for each object such as User, Account, Invoice, License, etc. All my integration tests pass when I run around the class, but many of them fail when shared gradle test. Most likely, there is a collision of data between the tests, and I'm not interested in wasting time on fixing data cleaning at the moment. I prefer to drop the database and context after each class. I decided that I could use it @DirtiesContextin the class, and so I annotated it with a test.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class, SecurityConfiguration.class},
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class InvoiceResourceIntTest {

I see that after adding annotation, the web application context is launched for each class, but when Liquibase is initialized, the requests do not start because the checksum matches. Since this is a database in memory, I expected the DB to be destroyed along with the Spring context, but this does not happen.

I also installed jpa hibernate ddl-auto in create-drop, but that didn't help. The next option that I am considering, instead mem, write h2db to a file and delete this file in @BeforeClass of my test integration file files. I prefer to automatically drop db in memory instead of controlling it in the test, but I want to try here as the last option. Thanks for the help.

Update:

I updated the test as shown below.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class, SecurityConfiguration.class},
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = "spring.datasource.name=AccountResource")
@DirtiesContext
public class AccountResourceIntTest {

. , , Liquibase .

app .yml

spring:
    datasource:
        driver-class-name: org.h2.Driver
        url: jdbc:h2:mem:myApp;DB_CLOSE_DELAY=-1
        name:
        username:
        password:
    jpa:
        database-platform: com.neustar.registry.le.domain.util.FixedH2Dialect
        database: H2
        open-in-view: false
        show_sql: true
        hibernate:
            ddl-auto: create-drop
            naming-strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
        properties:
            hibernate.cache.use_second_level_cache: false
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.hbm2ddl.auto: validate

JHipster 2.x, . . . AppProperties - ( Spring).

@Configuration
public class DatabaseConfiguration {

    private static final int LIQUIBASE_POOL_INIT_SIZE = 1;
    private static final int LIQUIBASE_POOL_MAX_ACTIVE = 1;
    private static final int LIQUIBASE_POOL_MAX_IDLE = 0;
    private static final int LIQUIBASE_POOL_MIN_IDLE = 0;

    private static final Logger LOG = LoggerFactory.getLogger(DatabaseConfiguration.class);

    /**
     * Creates data source.
     *
     * @param dataSourceProperties Data source properties configured.
     * @param appProperties the app properties
     * @return Data source.
     */
    @Bean(destroyMethod = "close")
    @ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
    @Primary
    public DataSource dataSource(final DataSourceProperties dataSourceProperties,
        final AppProperties appProperties) {

        LOG.info("Configuring Datasource with url: {}, user: {}",
            dataSourceProperties.getUrl(), dataSourceProperties.getUsername());

        if (dataSourceProperties.getUrl() == null) {
            LOG.error("Your Liquibase configuration is incorrect, please specify database URL!");
            throw new ApplicationContextException("Data source is not configured correctly, please specify URL");
        }
        if (dataSourceProperties.getUsername() == null) {
            LOG.error("Your Liquibase configuration is incorrect, please specify database user!");
            throw new ApplicationContextException(
                "Data source is not configured correctly, please specify database user");
        }
        if (dataSourceProperties.getPassword() == null) {
            LOG.error("Your Liquibase configuration is incorrect, please specify database password!");
            throw new ApplicationContextException(
                "Data source is not configured correctly, "
                    + "please specify database password");
        }

        PoolProperties config = new PoolProperties();
        config.setDriverClassName(dataSourceProperties.getDriverClassName());
        config.setUrl(dataSourceProperties.getUrl());
        config.setUsername(dataSourceProperties.getUsername());
        config.setPassword(dataSourceProperties.getPassword());
        config.setInitialSize(appProperties.getDatasource().getInitialSize());
        config.setMaxActive(appProperties.getDatasource().getMaxActive());
        config.setTestOnBorrow(appProperties.getDatasource().isTestOnBorrow());
        config.setValidationQuery(appProperties.getDatasource().getValidationQuery());

        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(config);
        LOG.info("Data source is created: {}", dataSource);

        return dataSource;

    }

    /**
     * Create data source for Liquibase using dba user and password provided for "liquibase"
     * in application.yml.
     *
     * @param dataSourceProperties Data source properties
     * @param liquibaseProperties Liquibase properties.
     * @param appProperties the app properties
     * @return Data source for liquibase.
     */
    @Bean(destroyMethod = "close")
    @ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
    public DataSource liquibaseDataSource(final DataSourceProperties dataSourceProperties,
        final LiquibaseProperties liquibaseProperties, final AppProperties appProperties) {

        LOG.info("Configuring Liquibase Datasource with url: {}, user: {}",
            dataSourceProperties.getUrl(), liquibaseProperties.getUser());

        /*
         * This is needed for integration testing. When we run integration tests using SpringJUnit4ClassRunner, Spring
         * uses
         * H2DB if it is in the class path. In that case, we have to create pool for H2DB.
         * Need to find a better solution for this.
         */
        if (dataSourceProperties.getDriverClassName() != null
            && dataSourceProperties.getDriverClassName().startsWith("org.h2.")) {
            return dataSource(dataSourceProperties, appProperties);
        }

        if (dataSourceProperties.getUrl() == null) {
            LOG.error("Your Liquibase configuration is incorrect, please specify database URL!");
            throw new ApplicationContextException("Liquibase is not configured correctly, please specify URL");
        }
        if (liquibaseProperties.getUser() == null) {
            LOG.error("Your Liquibase configuration is incorrect, please specify database user!");
            throw new ApplicationContextException(
                "Liquibase is not configured correctly, please specify database user");
        }
        if (liquibaseProperties.getPassword() == null) {
            LOG.error("Your Liquibase configuration is incorrect, please specify database password!");
            throw new ApplicationContextException(
                "Liquibase is not configured correctly, please specify database password");
        }

        PoolProperties config = new PoolProperties();

        config.setDriverClassName(dataSourceProperties.getDriverClassName());
        config.setUrl(dataSourceProperties.getUrl());
        config.setUsername(liquibaseProperties.getUser());
        config.setPassword(liquibaseProperties.getPassword());

        // for liquibase pool, we dont need more than 1 connection
        config.setInitialSize(LIQUIBASE_POOL_INIT_SIZE);
        config.setMaxActive(LIQUIBASE_POOL_MAX_ACTIVE);

        // for liquibase pool, we dont want any connections to linger around
        config.setMaxIdle(LIQUIBASE_POOL_MAX_IDLE);
        config.setMinIdle(LIQUIBASE_POOL_MIN_IDLE);

        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(config);
        LOG.info("Liquibase data source is created: {}", dataSource);

        return dataSource;

    }

    /**
     * Creates a liquibase instance.
     *
     * @param dataSource Data source to use for liquibase.
     * @param dataSourceProperties Datasource properties.
     * @param liquibaseProperties Liquibase properties.
     * @return Liquibase instance to be used in spring.
     */
    @Bean
    public SpringLiquibase liquibase(@Qualifier("liquibaseDataSource") final DataSource dataSource,
        final DataSourceProperties dataSourceProperties, final LiquibaseProperties liquibaseProperties) {

        // Use liquibase.integration.spring.SpringLiquibase if you don't want Liquibase to start asynchronously
        SpringLiquibase liquibase = new AsyncSpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:config/liquibase/master.xml");
        liquibase.setContexts(liquibaseProperties.getContexts());
        liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
        liquibase.setDropFirst(liquibaseProperties.isDropFirst());
        liquibase.setShouldRun(liquibaseProperties.isEnabled());

        return liquibase;

    }

}
+4
3

, H2 . (VM) foo, , foo, .

1.4.2 (. spring.datasource.generate-unique-name), 1.5.

@SpringBootTest(properties="spring.datasource.name=xyz"), xyz , .

+8

, liquibase . , , liquibase , , - . h2 @DirtiesContext, . Liquibase , , .

liquibase , application.yml ( ):

liquibase:
    contexts: test
    drop-first: true

:

liquibase.setDropFirst(true);

@DirtiesContext, , .

TestExecutionListener, . TestExecutionListener, .

public class CleanUpDatabaseTestExecutionListener
    extends AbstractTestExecutionListener {

    @Inject
    SpringLiquibase liquibase;

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public void afterTestClass(TestContext testContext) throws Exception {
        //This is a bit dirty but it works well
        testContext.getApplicationContext()
            .getAutowireCapableBeanFactory()
            .autowireBean(this);
        liquibase.afterPropertiesSet();
    }

TestExecutionListener, :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@TestExecutionListeners(listeners = {
    DependencyInjectionTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    CleanUpDatabaseTestExecutionListener.class,
})
public class Test {
    //your tests
}

: @DirtiesContext TestExecutionListener , .

0

username, url password.

spring:
 autoconfigure:
  exclude: org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
 jackson:
  serialization:
   indent_output: true
 datasource:
  driver-class-name: org.hsqldb.jdbcDriver
  generate-unique-name: true
 jpa:
  hibernate:
    dialect: org.hibernate.dialect.HSQLDialect
    ddl-auto: validate
  show-sql: true
 h2:
  console:
   enabled: false

liquibase:
 change-log: classpath:/liquibase/db.changelog-master.xml
 drop-first: true
 contexts: QA
0

All Articles