본문 바로가기

카테고리 없음

[Spring] mybatis를 통한 junit 테스트

1) spring-mvc.xml 

: 객체를 만들어서 컨테이너에 생성하게 하는 xml 코드

2) MyBatisTest.java

: myBatis 를 통한 Junit 테스트 예시

package kr.co.mlec;

import static org.junit.Assert.assertNotNull;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//@RunWith(SpringJunit4ClassRunner.class): Spring으로 Junit 테스트를 진행하기 위해 있어야 함
//@ContextConfiguration : spring-mvc.xml에 있는 객체들을 컨테이너에 쭉 올려놓는 작업 (Junit테스트에서)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/spring/spring-mvc.xml"})
public class MyBatisTest {
	
	@Autowired
	private DataSource ds;
	
	@Test
	public void DB접속테스트() throws Exception {
		assertNotNull(ds);
	}
}

 

 

3) MyProjectTest.java

package kr.co.mlec;

import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import kr.co.mlec.board.dao.BoardDAO;
import kr.co.mlec.board.dao.BoardDAOImpl;
import kr.co.mlec.board.service.BoardService;
import kr.co.mlec.board.vo.BoardVO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/spring/spring-mvc.xml"})
public class MyProjectTest {
	
    //@Autowired: bean으로 객체 주입할 때 쓰는 annotation
	@Autowired
	private BoardService service;
	
	@Autowired
	private BoardDAO dao;
	
    //@Test : 테스트 할 메소드에 @Test
	@Test
	public void SERVICE_상세페이지조회() throws Exception{
		BoardVO board = dao.selectByNo(3);
		System.out.println(board);
	}
	
    //@Ignore : 테스트 시 메소드 무시할 때 쓰임//테스트 시 반영 X
	@Ignore
	@Test
	public void DAO_상세페이지조회() throws Exception{
		BoardVO board = dao.selectByNo(7);
		System.out.println(board);
	}
	
    
	@Ignore
	@Test
	public void SERVICE_전체게시글조회() throws Exception{
		List<BoardVO> list = service.selectAllBoard();
		for(BoardVO board : list) {
			System.out.println(board);
		}
	}
	
	@Ignore
	@Test
	public void 전체게시글조회() throws Exception{
		List<BoardVO> list = dao.selectAll();
		
		for(BoardVO board: list) {
			System.out.println(board);
		}
	}
}