스프링부트 단위테스트

Tae hyun·2023년 2월 8일
0

단위테스트
스프링에서는 ExtendWith와 ContextConfiguration 어노테이션이 필요했다.

package com.my.board.dao;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.my.board.vo.Board;
import com.my.board.vo.BoardReply;
import com.my.customer.vo.Customer;
import com.my.exception.AddException;
import com.my.exception.FindException;

@ExtendWith(SpringExtension.class)  //스프링 ApplicateionContext구동
@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/myApplicationContext2.xml")
class BoardDAOOracleTest {
	@Autowired
	private BoardDAO dao;

	@DisplayName("첫번째게시글 테스트")
	@Test
	void testSelectAll() throws FindException {
		int expectedBoardSize = 2;
		List<Board> list = dao.selectAll();
		//assertEquals : jnit 테스트에서만 사용할 수 있는 메서드, 단정 짓는다는 뜻
		assertEquals(expectedBoardSize, list.size());
		
		int expectedBoardNum = 2;  //첫글의 글번호
		String expectedBoardId = "id1"; //첫글의 작성자
		String expectedBoardTitle = "글2"; //첫글의 제목
		int expectedReplySize = 0; //첫글의 댓글 개수 (댓글이 없는 글)
//		Integer expectedReplyNum = null;  //첫글의 댓글번호
		
		Board bFirst = list.get(0);
		assertEquals(expectedBoardNum, bFirst.getBoardNum());
		assertEquals(expectedBoardId, bFirst.getBoardC().getId());
		assertEquals(expectedBoardTitle, bFirst.getBoardTitle());
		assertNotNull(bFirst.getBoardContent());
		assertEquals(expectedReplySize,bFirst.getReplies().size());
		
	}
	
	@DisplayName("두번째게시글 테스트")
	@Test
	void testSelectAllSecond() throws FindException {
		int expectedBoardSize = 2;
		List<Board> list = dao.selectAll();
		//assertEquals : jnit 테스트에서만 사용할 수 있는 메서드, 단정 짓는다는 뜻
		assertEquals(expectedBoardSize, list.size());
		
		int expectedBoardNum = 1;  //두번째의 글번호
		String expectedBoardId = "id1"; //두번째글의 작성자
		String expectedBoardTitle = "글1"; //첫글의 제목
		int expectedReplySize = 3; //두번째글의 댓글 개수
		
		Board bSecond = list.get(1); //두번째글
		assertEquals(expectedBoardNum, bSecond.getBoardNum());
		assertEquals(expectedBoardId, bSecond.getBoardC().getId());
		assertEquals(expectedBoardTitle,bSecond.getBoardTitle());
		assertNotNull(bSecond.getBoardContent());
		assertEquals(expectedReplySize,bSecond.getReplies().size());
		
		BoardReply reply = bSecond.getReplies().get(0); //댓글 첫번째
		int expectedReplyNum = 2; //댓글 번호
		assertEquals(expectedReplyNum, reply.getBoardReplyNum());
		assertNull(reply.getBoardReplyParentNum()); //댓글은 parent_num이 null이다
		                                            //대댓글들은 parent_num이 null이다
		
		
	}
	@Test
	void testInsertBoard() throws AddException {
		Board b = new Board();
		String id = "id1"; Customer c = new Customer(); c.setId(id);
		b.setBoardC(c);
		b.setBoardTitle("제목1");
		b.setBoardContent("내용1");
		dao.insert(b);
	}

}

스프링부트에서는 SpringBootTest 어노테이션만 있으면 된다

단위테스트할때 @Transactional이 붙어있으면 autocommit이 false가 됨

@Test
	@Transactional
	void testInsertBoard() throws AddException {
		Board b = new Board();
		String id = "id1"; Customer c = new Customer(); c.setId(id);
		b.setBoardC(c);
		b.setBoardTitle("제목1");
		b.setBoardContent("내용1");
		dao.insert(b);
	}

profile
안녕하세요 개발하는 알파카입니다. https://github.com/oh-taehyun

0개의 댓글