springtestdbunit / spring-test-dbunit

Integration between the Spring testing framework and DBUnit
http://springtestdbunit.github.com/spring-test-dbunit/
Apache License 2.0
476 stars 238 forks source link

Custom PostgresqlDataTypeFactory #147

Closed raderio closed 6 years ago

raderio commented 6 years ago

How to add custom PostgresqlDataTypeFactory? http://dbunit.sourceforge.net/apidocs/org/dbunit/ext/postgresql/PostgresqlDataTypeFactory.html

lvv83 commented 6 years ago
package com.example;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.springtestdbunit.bean.DatabaseConfigBean;
import com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean;
import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;

@Configuration
public class TestContext {

    @Bean
    DatabaseConfigBean databaseConfig()
    {
        DatabaseConfigBean config = new DatabaseConfigBean();

        config.setCaseSensitiveTableNames(true);
        config.setDatatypeFactory(new PostgresqlDataTypeFactory());

        return config;
    }

    @Bean
    DataSource dataSource() {
        // your DataSource configuration
    }

    @Bean(name = "dbunitConnection")
    DatabaseDataSourceConnectionFactoryBean connectionFactory(DataSource dataSource, DatabaseConfigBean databaseConfig)
    {
        DatabaseDataSourceConnectionFactoryBean factory = new DatabaseDataSourceConnectionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setDatabaseConfig(databaseConfig);

        return factory;
    }
}

And then

package com.example;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DbUnitConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestContext.class }, loader = AnnotationConfigContextLoader.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class})
@DbUnitConfiguration(databaseConnection = "dbunitConnection")
public abstract class AbstractDbUnitTest {
}
raderio commented 6 years ago

Thanks. As I understand this dependency is only for test, but I need this not only for tests. Can you please help?

lvv83 commented 6 years ago

Well, I use dbUnit and springtestdbunit only for tests with <scope>test</scope> in maven.

Datasource bean should be moved to another ProductionContext. It mentioned here only for example. So why this configuration should be used in production?

raderio commented 6 years ago

Yeap, you are right, I thought about something else.