~홍~

Junit test 본문

Spring/-

Junit test

~홍~ 2021. 4. 5. 10:45
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
Comments