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
- append()
- SPRING 특징
- JDK 설치
- innerclass
- pox.xml 오류
- Statement
- Spring 개념
- 톰켓
- tomat
- 자바 개발환경준비
- File
- Runnable
- Overloading
- OuterClass
- singleton
- Visuall code
- 접근제한수식어
- 싱글톤 패턴
- 오버로딩
- spring 페이징
- 드림코딩
- New Dialog
- 오라클 데이터베이스
- Unbox
- inheritance
- protected
- java
- Spring 구조
- New Frame
- DB 설정
Archives
- Today
- Total
~홍~
Junit test 본문
728x90
Java Resources >> src/test/java >> com.spring.board >> OracleJDBCTest.class 생성
- OracleJDBCTest / Junit 테이스
package com.spring.board;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import oracle.jdbc.driver.OracleDriver;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})
@WebAppConfiguration
public class OracleJDBCTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(OracleJDBCTest.class);
private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
private static final String USER = "board";
private static final String PASSWORD = "0000";
@Test
public void testOracleConnect() {
Connection conn = null;
try {
DriverManager.registerDriver(new OracleDriver());
conn = DriverManager.getConnection(URL, USER, PASSWORD);
LOGGER.info("oracle 연결 성공");
} catch (SQLException e) {
LOGGER.error("oracle 연결 실패 : " + e.getMessage());
} finally {
try {
conn.close();
LOGGER.info("oracle 연결 해제 성공");
} catch (SQLException e) {
LOGGER.error("oracle 연결 해제 실패 : " + e.getMessage());
}
}
}
} // end OracleJDBCTest
- DataSourceTest / Junit 테이스
package com.spring.board;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})
@WebAppConfiguration
public class DataSourceTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(DataSourceTest.class);
// Spring Framework가 관리하는 DataSource 객체를 주입받음
// - root-context.xml에서 bean으로 선언된 DataSource 객체를 주입받음
@Autowired
private DataSource ds;
@Test
public void testDataSource() {
Connection conn = null;
try {
conn = ds.getConnection();
LOGGER.info("DataSource 연결 성공");
} catch (SQLException e) {
LOGGER.error("DataSource 연결 실패 : " + e.getMessage());
} finally {
try {
conn.close();
LOGGER.info("DataSource 연결 반환 성공");
} catch (SQLException e) {
LOGGER.error("DataSource 연결 반환 실패 : " + e.getMessage());
}
}
}
} // end DataSourceTest
- SqlSessionTest / Junit 테이스
package SqlSessionTest;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.spring.board.domain.BoardVO;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class SqlSessionTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(SqlSessionTest.class);
private static final String NAMESPACE =
"com.spring.board.BoardMapper";
@Autowired
private SqlSession sqlSession;
@Test
public void testInsert() {
BoardVO vo = new BoardVO(0, "test", "test", "test", null, 0, "test", 0);
int result = sqlSession.insert(NAMESPACE + ".board_insert", vo); // .board_insert : mapper.xml의 id
LOGGER.info(result + "행 삽입");
}
} // end SqlSessionTest
- BoardDAOTest / Junit
package com.spring.board;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.spring.board.domain.BoardVO;
import com.spring.board.persistence.BoardDAO;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class BoardDAOTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(BoardDAOTest.class);
@Autowired
private BoardDAO dao;
@Test
public void testDAO() throws Exception {
//ok testInsert();
//ok testSelectBybNo();
//ok testSelectAll();
}
private void testInsert() throws Exception {
BoardVO vo = new BoardVO(0, "test", "test", "test", null, 0, "test", 0);
int result = dao.insertBoard(vo);
if(result == 1) {
LOGGER.info("insert 성공");
} else {
LOGGER.info("insert 실패");
}
}
private void testSelectAll() throws Exception {
List<BoardVO> list = dao.selectBoard();
for (BoardVO vo : list) {
LOGGER.info(vo.toString());
}
} // end testSelectAll()
private void testSelectBybNo() throws Exception {
BoardVO vo = dao.selectBoard(100000);
if(vo != null) {
LOGGER.info(vo.toString());
} else {
LOGGER.info("데이터 없음");
}
}
} // end BoardDAOTest
- BoardControllerTest / Junit
package com.spring.board;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class BoardControllerTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(BoardControllerTest.class);
@Autowired
private WebApplicationContext wac; // 웹 어플리케이션 객체
// MVC 패턴의 앱을 테스트하는 mock-up 객체
private MockMvc mock;
@Before
// 실제 JUnit 테스트를 실행하기 전에 초기화 작업을 수행하는 메소드
public void beforeTest() {
LOGGER.info("beforeTest 호출");
LOGGER.info("wac : " + wac);
LOGGER.info("mock : " + mock);
// 컨트롤러 메소드에게 요청을 보낼 수 있는 mockup 객체 생성
mock = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
// JUnit 테스트를 수행하는 메소드
public void test() throws Exception {
testList();
}
private void testList() throws Exception {
LOGGER.info("testList() 호출");
// get(uri) : GET요청에 대한 mock 객체 생성
// post(uri) : POST요청에 대한 mock 객체 생성
// put(uri) : PUT요청에 대한 mock 객체 생성
// delete(uri) : DELETE요청에 대한 mock 객체 생성
mock.perform(get("/bqna/list").param("page", "1"));
}
@After
// JUnit 테스트 수행 후 호출되는 메소드
public void afterTest() {
LOGGER.info("afterTest() 호출");
}
} // end BoardControllerTest
'Spring > -' 카테고리의 다른 글
Spring 게시판 (5) _ 페이징 구현 (0) | 2021.04.09 |
---|---|
Spring 게시판 (4) _ nav, 게시글 삭제 (0) | 2021.04.08 |
Spring 게시판 (3) _ 게시판 전체리스트 / 글작성 / 게시물 상세 정보 / 게시글 수정 (0) | 2021.04.05 |
Spring 게시판 (2) DB 테이블 생성 및 스프링 연결 (0) | 2021.04.04 |
스프링 게시판 만들기 ( 1 ) 기본 설정 (0) | 2021.04.04 |
Comments