Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- mac
- 대전본식영상
- c#코딩의기술실전편
- dbContext
- extjs
- 라도무스dvd
- 코드프로그래머스
- 명시적외래키
- React
- ORM
- error
- Config
- 스냅잘찍음
- 에스가든스냅
- c#
- Request
- JavaScript
- Store
- LINQ
- 상속
- JSON
- EFCore
- .net
- scanner
- minimalAPI
- ViewModel
- lazy loading
- intellij
- extraParams
- vscode
Archives
- Today
- Total
ejyoo's 개발 노트
[JUnit4, MyBatis] 작성한 CRUD SQL 에 대해 테스트 코드 작성 본문
환경
1. JUnit 4
2. JDK 1.8
3. DBCP 2.5
4. MyBatis 3.5
5. Ojdbc6
사전준비 - MyBatis SqlSessionFactory 클래스 생성
package ejyoo.servlet.util;
import java.io.Reader;
import java.sql.Connection;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.TransactionIsolationLevel;
public class OracleMyBatisSqlSessionFactory implements SqlSessionFactory{
private SqlSessionFactory sqlSessionFactory;
{
String config = "kr/or/ddit/mybatis/sqlConfig.xml";
try {
Reader reader = Resources.getResourceAsReader(config);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
System.out.println("sqlSessionFactory 성공했습니다.");
} catch(Exception e) {
System.out.println("sqlSessionFactory 실패했습니다.");
e.printStackTrace();
}
}
@Override
public Configuration getConfiguration() {
return sqlSessionFactory.getConfiguration();
}
@Override
public SqlSession openSession() {
return sqlSessionFactory.openSession(true);
}
@Override
public SqlSession openSession(boolean arg0) {
return sqlSessionFactory.openSession(arg0);
}
@Override
public SqlSession openSession(Connection arg0) {
return sqlSessionFactory.openSession(arg0);
}
@Override
public SqlSession openSession(TransactionIsolationLevel arg0) {
return sqlSessionFactory.openSession(arg0);
}
@Override
public SqlSession openSession(ExecutorType arg0) {
return sqlSessionFactory.openSession(arg0);
}
@Override
public SqlSession openSession(ExecutorType arg0, boolean arg1) {
return sqlSessionFactory.openSession(arg0, arg1);
}
@Override
public SqlSession openSession(ExecutorType arg0, TransactionIsolationLevel arg1) {
return sqlSessionFactory.openSession(arg0, arg1);
}
@Override
public SqlSession openSession(ExecutorType arg0, Connection arg1) {
return sqlSessionFactory.openSession(arg0, arg1);
}
}
사전준비 - sqlConfig.xml Builder로 로딩 (SqlConfig, Mapper 로드)
- implements SqlSessionFactory 한 클래스 생성
- sqlConfig.xml(SqlConfig, Mapper) 가져와서 Builder로 로딩
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="kr/or/ddit/properties/db.properties" />
<typeAliases>
<typeAlias type="java.lang.String" alias="str" />
<typeAlias type="java.lang.Integer" alias="int" />
<typeAlias type="ejyoo.dto.MemberVO" alias="member" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- pool setting -->
<property value="2" name="poolMaximumActiveConnections" />
<property value="2" name="poolMaximumIdleConnections" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="kr/or/ddit/mybatis/Member-Mapper.xml" />
</mappers>
</configuration>
테스트 코드 작성할 클래스 생성
@Before, @After 에 Session 시작 삭제 코드 작성
@Before
public void openSession() {
session = factory.openSession();
}
@After
public void closeSession() {
session.close();
}
모든 쿼리 테스트 코드 작성
@Test
public void selectMemberListSQL() {
MemberVO paramTypeMemberVo = new MemberVO();
paramTypeMemberVo.setUserNo(124);
paramTypeMemberVo.setUserId("test3");
paramTypeMemberVo.setUserPw("yej33585");
paramTypeMemberVo.setUserPhone("010-0000-0000");
paramTypeMemberVo.setUserEmail("test@test.com");
List<MemberVO> memberVoList = session.selectList("Member-Mapper.selectMemberList",paramTypeMemberVo);
Assert.assertTrue(memberVoList.size()>0);
}
@Test
public void selectMemberByInfoSQL() {
MemberVO paramTypeMemberVo = new MemberVO();
paramTypeMemberVo.setUserNo(124);
paramTypeMemberVo.setUserId("test3");
paramTypeMemberVo.setUserPw("yej33585");
paramTypeMemberVo.setUserPhone("010-0000-0000");
paramTypeMemberVo.setUserEmail("test@test.com");
MemberVO memberVo = session.selectOne("Member-Mapper.selectMemberById",paramTypeMemberVo);
Assert.assertEquals("test3", memberVo.getUserId());
}
@Test
public void selectMemberByIdSQL() {
String userId = "test3";
MemberVO memberVo = session.selectOne("Member-Mapper.selectMemberById",userId);
Assert.assertEquals("test3", memberVo.getUserId());
}
@Test
public void insertMemberByInfoSQL() {
MemberVO paramTypeMemberVo = new MemberVO();
paramTypeMemberVo.setUserId("test3");
paramTypeMemberVo.setUserPw("yej33585");
paramTypeMemberVo.setUserPhone("010-0000-0000");
paramTypeMemberVo.setUserEmail("test@test.com");
int cnt = session.insert("Member-Mapper.insertMemberByInfo", paramTypeMemberVo);
Assert.assertEquals(1, cnt);
}
@Test
public void updateMemberByInfoSQL() {
MemberVO paramTypeMemberVo = new MemberVO();
paramTypeMemberVo.setUserNo(124);
paramTypeMemberVo.setUserId("test4");
paramTypeMemberVo.setUserPw("yej3585");
paramTypeMemberVo.setUserPhone("010-1111-1111");
paramTypeMemberVo.setUserEmail("test@test.com");
int cnt = session.update("Member-Mapper.updateMemberByInfo", paramTypeMemberVo);
Assert.assertTrue(cnt == 1);
}
@Test
public void deleteMemberByIdSQL() {
String userId = "test3";
int cnt = session.update("Member-Mapper.deleteMemberById", userId);
Assert.assertTrue(cnt == 1);
}
'BackEnd > JUnit' 카테고리의 다른 글
[Junit4] 테스트 메소드 실행 순서 결정 (0) | 2021.05.19 |
---|---|
[JUnit4] 단정문 및 어노테이션 정리 (0) | 2021.05.19 |
[JUnit4] 사용법 -> 다른 블로그 링크 (0) | 2021.05.19 |
[JUnit4] 단위 테스트 라이브러리 추가 및 코드 (0) | 2021.05.18 |