Skip to content

Instantly share code, notes, and snippets.

@zaenk
Created April 1, 2017 17:47
Show Gist options
  • Save zaenk/71e2e1ee5340fa19a4fcd3b67b003a59 to your computer and use it in GitHub Desktop.
Save zaenk/71e2e1ee5340fa19a4fcd3b67b003a59 to your computer and use it in GitHub Desktop.
Minimal configuration file for integration tests with Spring and MariaDb4j
import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import javax.sql.DataSource;
import java.sql.Driver;
@Lazy
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class TestDbConfig {
@Value("${db.port} ?: 3306")
private Integer port;
private static final String DB_SERVICE = "dbServiceBean";
@Bean(name = {DB_SERVICE})
public MariaDB4jSpringService mariaDb() {
MariaDB4jSpringService mariaDb = new MariaDB4jSpringService();
mariaDb.setDefaultPort(port);
return mariaDb;
}
/**
* If you want to use Flyway/Liqubase, make sure those beans are also depend
* on the MariaDB service or this bean.
*
* @param dataSourceProperties configured in application.properties, spring.datasource.*
* @return
* @throws ClassNotFoundException
*/
@Bean
@DependsOn(DB_SERVICE)
public DataSource dataSource(DataSourceProperties dataSourceProperties) throws ClassNotFoundException {
final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(getDriverClassByName(dataSourceProperties.determineDriverClassName()));
dataSource.setUrl(dataSourceProperties.getUrl());
dataSource.setUsername(dataSourceProperties.getUsername());
dataSource.setPassword(dataSourceProperties.getPassword());
return dataSource;
}
@SuppressWarnings("unchecked")
private Class<Driver> getDriverClassByName(String className) {
try {
return (Class<Driver>) Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager manager = new DataSourceTransactionManager();
manager.setDataSource(dataSource);
return manager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment