진이의 Developer Story

JUnit MyBatis 테스트 본문

Java/Spring

JUnit MyBatis 테스트

JIN3260 2016. 1. 20. 15:24
package com.sample.board;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.sample.board.service.BoardService;
import com.sample.board.vo.BoardVO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
		"file:src/main/webapp/WEB-INF/spring/mapper-context.xml",
		"file:src/main/webapp/WEB-INF/spring/datasource-context.xml" })
@WebAppConfiguration
public class BoardServiceTest {
	@Autowired
	private ApplicationContext applicationContext;

	@Resource(name = "boardServiceImpl")
	private BoardService boardService;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void deleteBoardTest() throws Exception {
		System.out.println("deleteBoardTest()");

		List list = boardService.getBoardList();
		Iterator it = list.iterator();

		while (it.hasNext()) {
			BoardVO boardVO = it.next();
			boardService.deleteBoard(boardVO);
			System.out.println(boardVO.getIdx() + "번글 삭제성공");
		}
	}

	@Test
	public void insertBoardTest() {
		System.out.println("insertBoardTest()");

		BoardVO boardVO = new BoardVO();
		boardVO.setTitle("BoardServiceTest");
		boardVO.setId("jin3260");
		boardVO.setContent("http://jin3260.tistory.com/");
		Assert.assertEquals(boardService.insertBoard(boardVO), 1);

		BoardVO boardVO2 = new BoardVO();
		boardVO2.setTitle("진이의 Developer Story");
		boardVO2.setId("jin3260");
		boardVO2.setContent("http://jin3260.tistory.com/");
		boardService.insertBoard(boardVO2);
	}

	@Test
	public void getBoardListTest() {
		System.out.println("getBoardListTest()");

		List list = boardService.getBoardList();
		Iterator it = list.iterator();

		while (it.hasNext()) {
			BoardVO boardVO = it.next();

			System.out.println("=======================================");
			System.out.println("IDX     : " + boardVO.getIdx());
			System.out.println("TITLE   : " + boardVO.getTitle());
			System.out.println("Content : " + boardVO.getContent());
			System.out.println("ID      : " + boardVO.getId());
			System.out.println("HIT     : " + boardVO.getHit());
			System.out.println("DATE    : " + boardVO.getDate());
			System.out.println("=======================================");
		}
	}
}

아래는 테스트 후 콘솔 출력과 JUnit 결과입니다.

INFO : org.springframework.test.context.web.WebTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.web.WebTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1f7a3d0, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@123e82b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5f62b6, org.springframework.test.context.transaction.TransactionalTestExecutionListener@49bed2, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@18edd8d]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/mapper-context.xml]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/datasource-context.xml]
INFO : org.springframework.web.context.support.GenericWebApplicationContext - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@46f93: startup date [Wed Jan 20 15:20:13 KST 2016]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/insertBoard.do],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.sample.board.controller.BoardController.insertBoard(com.sample.board.vo.BoardVO)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getBoardList.do],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.sample.board.controller.BoardController.getBoardList()
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getBoardView.do],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.sample.board.controller.BoardController.getBoardView(int)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/insertBoardForm.do],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.sample.board.controller.BoardController.insertBoardForm(com.sample.board.vo.BoardVO)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@46f93: startup date [Wed Jan 20 15:20:13 KST 2016]; root of context hierarchy
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@46f93: startup date [Wed Jan 20 15:20:13 KST 2016]; root of context hierarchy
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
deleteBoardTest()
insertBoardTest()
getBoardListTest()
===================================
IDX     : 3
TITLE   : BoardServiceTest
Content : http://jin3260.tistory.com/
ID      : jin3260
HIT     : 0
DATE    : 2016-01-20 15:20:14.0
===================================
===================================
IDX     : 4
TITLE   : 진이의 Developer Story
Content : http://jin3260.tistory.com/
ID      : jin3260
HIT     : 0
DATE    : 2016-01-20 15:20:14.0
===================================
INFO : org.springframework.web.context.support.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@46f93: startup date [Wed Jan 20 15:20:13 KST 2016]; root of context hierarchy


성공적으로 수행되었음을 알 수 있습니다.

Comments