Steven Bayer
Steven Bayer

Reputation: 2137

Spring Boot component test with SQL query

I'm trying to setup a component test for a sql query. To handle the SQL I am using myBatis.

I have a test dataSource config that is creating an embedded H2 database. This is working well for any simple query.

My problem is I have a query that uses SQL common table expression that H2 doesn't support (NOLOCK).

Can I either have H2 ignore the NOLOCK part or replace the query string used during junit testing?

myBatis mapper looks something like this:

public interface CoolMapper {
    @Results(...)
    @Select("SELECT * FROM cool_table WITH (NOLOCK)")
    List<CoolObject> getThings();
}

Data Source config: @Configuration public class TestingDataSourceConfig {

    public TestingDataSourceConfig() {}

    @Profile("test")
    @Bean(name = "dataSources")
    @Primary
    @Qualifier(DataSourceConfig.DATA_SOURCES)
    public Map<UUID, DataSource> dataSources() {
        Map<UUID, DataSource> dataSourceMap = new HashMap<>();
        UUID uuid = UUID.fromString("dac46f50-a1fb-3434-b262-0c0c4564b117");

        EmbeddedDatabase database = new EmbeddedDatabaseBuilder().generateUniqueName(true)
                .setType(EmbeddedDatabaseType.H2)
                .setScriptEncoding("UTF-8")
                .ignoreFailedDrops(true)
                .addDefaultScripts()
                .build();

        dataSourceMap.put(uuid, database);

        return dataSourceMap;
    }
}

Upvotes: 1

Views: 1570

Answers (1)

Bohemian
Bohemian

Reputation: 425258

Sometimes you just need a real runtime environment, like here where you need a proprietary database. In these cases, don't attempt to do this as a unit test. Do it as part of your acceptance/system testing - ie the tests you run on your application once deployed to your dev (or QA, whatever) server.

Upvotes: 1

Related Questions