mybatis mapper junit test

■ spring기반 mybatis를 junit으로 테스트하기 위한 pom.xml dependency 설정

<project>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.10.19</version>
    </dependency>
  <dependencies>
</project>

 

■ SampleDomain.java

public class SampleDomain {
        private String sampleId ;
        private String sampleNm ;
        public String getSampleId() {
               return sampleId ;
       }
        public void setSampleId(String sampleId) {
               this.sampleId = sampleId ;
       }
        public String getSampleNm() {
               return sampleNm ;
       }
        public void setSampleNm(String sampleNm) {
               this.sampleNm = sampleNm ;
       }
}

 

■ SampleMapper.java

import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Param;

@Repository
public interface SampleMapper {

    public SampleDomain viewSample(
        @Param("param") SampleDomain param
    );
}

 

■ SampleMapper.xml

<?xml version= "1.0" encoding ="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="net.iotinfra.pilot1.mappertest1.SampleMapper" >

        <resultMap id= "sampleResult" type="net.iotinfra.pilot1.mappertest1.SampleDomain" >
               <result property= "sampleId"       column ="SAMPLE_ID" />
               <result property= "sampleNm"       column ="SAMPLE_NM" />
        </resultMap>
              
        <select id= "viewSample"
                      parameterType="net.iotinfra.pilot1.mappertest1.SampleDomain"
                      resultMap="sampleResult" >
              SELECT /* SampleMapper.xml viewSample */
                     SAMPLE_ID
                   , SAMPLE_NM
                FROM T_SAMPLE
               WHERE 1=1
                 AND SAMPLE_ID                  = #{param.sampleId}
        </select>       
</mapper>

 

■ jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/fin?autoReconnect=true
jdbc.username=nextman
jdbc.password=aaa

 

■ datasource-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  	xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://mybatis.org/schema/mybatis-spring 
		http://mybatis.org/schema/mybatis-spring.xsd">  
  
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:/properties/jdbc.properties</value>
		</property>
	</bean>
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
<!-- 		<property name="initialSize" 	value="${jdbc.min.size}" /> -->
<!-- 		<property name="maxActive" 		value="${jdbc.max.size}" /> -->
<!-- 		<property name="maxIdle" 		value="${jdbc.max.idle}" /> -->
<!-- 		<property name="minIdle" 		value="${jdbc.min.idle}" /> -->
<!-- 		<property name="defaultAutoCommit" value="false"/> -->
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource"	ref="dataSource"/>
		<property name="configLocation" value="classpath:/spring/mybatis-config.xml"/>
 		<property name="mapperLocations" value="classpath*:net/iotinfra/**/*.xml" />
	</bean>
    
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  		<property name="basePackage" value="net.iotinfra.pilot1" /> 
  		<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
	</bean>

	<bean id="transactionManager" 	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory"/>
	</bean>
	
</beans>

 

■ TestRunner를 org.springframework.test.context.junit4.SpringJUnit4ClassRunner로 지정하고 DB연결을 위해서 datasource-config.xml을 지정하였다.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
        "classpath:spring/datasource-config.xml"
})
public class SampleMapperTest {

        @Autowired
        private SampleMapper sampleMapper ;
       
        private SampleDomain sampleDomain ;
       
        @Before
        public void setUp() {
               sampleDomain = new SampleDomain();
               sampleDomain.setSampleId("sampleId1" );
               sampleDomain.setSampleNm("sampleNm1" );
       }

        @Test
        public void testViewSample() {
              
              SampleDomain sd = sampleMapper.viewSample(sampleDomain );
              System. out.println( "[mirinae.maru] sample.sampleId : " + sd.getSampleId() );
              System. out.println( "[mirinae.maru] sample.sampleNm : " + sd.getSampleNm() );

               assertEquals( "", sd.getSampleId(), sampleDomain.getSampleId() );
               assertEquals( "", sd.getSampleNm(), sampleDomain.getSampleNm() );
       }

}

 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다