develog

[junit] spring junit 설정2 본문

Dev/junit

[junit] spring junit 설정2

냐옴 2019. 8. 2. 15:54

@ContextConfiguration(classes = {}) 를 사용

TestBase.java

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class, AppConfig_oracle.class, AppConfig_mssql.class})
@WebAppConfiguration
public class TestBase {
    
    @Autowired
    protected ApplicationContext applicationContext;
    
    public SqlSession getSqlSession_oracle() {
        return (SqlSession) applicationContext.getBean("oracle");
    }
    
    public SqlSession getSqlSession_mssql() {
        return (SqlSession) applicationContext.getBean("mssql");
    }
    
    public  T getDao_oracle(Class type) {
        return (T) getSqlSession_oracle().getMapper(type);
    }
    
    public  T getDao_mssql(Class type) {
        return (T) getSqlSession_mssql().getMapper(type);
    }
    
    public  T getBean(Class type) {
        return (T) applicationContext.getBean(type);
    }
    
    public  T getBean(String beanName) {
        return (T) applicationContext.getBean(beanName);
    }
    
    public String getSql(String mapId, Object param) {
        SqlSession sqlSession = getSqlSession_oracle();
        Configuration configuration = sqlSession.getConfiguration();
        MappedStatement mappedStatement = configuration.getMappedStatement(mapId);
        BoundSql boundSql = mappedStatement.getBoundSql(param);
        String sql = boundSql.getSql();
        return sql;
    }

}

AppConfig.java

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"aa.bb.cc"})
public class AppConfig {
    
    @Bean(name = "globalContext")
    public PropertiesFactoryBean globalContext() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new FileSystemResource("src/test/resources/properties/global-context-test.properties"));
        return bean;
    }
    
    @Bean(name = "dbContext")
    public PropertiesFactoryBean dbContext() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new FileSystemResource("src/test/resources/properties/db-context-test.properties"));
        return bean;
    }

}

AppConfig_oracle.java

import java.io.IOException;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"aa.bb.cc"})
public class AppConfig_oracle {
    
    /* oracle */
    @Bean(name = "dataSource_oracle")
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:mydb");
        dataSource.setUsername("myname");
        dataSource.setPassword("mypass");
        return dataSource;
    }
    @Bean(name = "sqlSessionFactory_oracle")
    public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext, @Qualifier("dataSource_oracle") @Autowired DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis/mybatis-config.xml"));
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/oracle/**/*.xml"));
        return sqlSessionFactoryBean;
    }
    @Bean(name = "oracle")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory_oracle") @Autowired SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    @Bean(name = "txManager_oracle")
    public DataSourceTransactionManager txManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }    

}

AppConfig_mssql.java

import java.io.IOException;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"aa.bb.cc"})
public class AppConfig_mssql {
    
    /* mssql */
    @Bean(name = "dataSource_mssql")
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setUrl("jdbc:sqlserver://127.0.0.1;databaseName=mydb");
        dataSource.setUsername("myname");
        dataSource.setPassword("mypass");
        return dataSource;
    }
    @Bean(name = "sqlSessionFactory_mssql")
    public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext, @Qualifier("dataSource_mssql") @Autowired DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis/mybatis-config.xml"));
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/mssql/**/*.xml"));
        return sqlSessionFactoryBean;
    }
    @Bean(name = "mssql")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory_mssql") @Autowired SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    @Bean(name = "txManager_mssql")
    public DataSourceTransactionManager txManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }

}

'Dev > junit' 카테고리의 다른 글

[Junit] Test Assertion  (0) 2019.12.23
[junit4] pom.xml  (0) 2019.08.27
[junit] spring junit 설정1  (0) 2019.08.02
[JUnit] JUnit Transaction Rollback  (0) 2015.05.06
JUnit 에서 ApplicationContext 접근  (0) 2013.12.11
Comments