-
[Spring Boot] REST API 만들기(5) - Mapper 구현 및 단위 테스트(Junit4)Spring Boot/2.4.x - REST API 만들기 2020. 5. 6. 15:58반응형
게시글 목록 조회, 상세 조회, 등록, 수정, 삭제를 위한 BoardMapper.xml, BoardMapper.java 를 구현한 후 JUnit으로 테스트를 진행하겠습니다.
1. Mapper 구현
BoardMapper.xml
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.api.board.mapper.BoardMapper"><select id="getBoardList" resultType="com.api.board.domain.Board">SELECT *FROM (SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDORDER BY BOARD_RE_REF DESC, BOARD_RE_SEQ ASC) T1</select><select id="getBoardDetail" parameterType="int" resultType="com.api.board.domain.Board">SELECT T1.*FROM (SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDWHERE BOARD_SEQ = #{board_seq}) T1</select><insert id="insertBoard" parameterType="com.api.board.domain.Board"><selectKey resultType="int" keyProperty="board_seq" order="AFTER">SELECT LAST_INSERT_ID()</selectKey>INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(0, 0, 0, #{board_writer}, #{board_subject}, #{board_content}, 'TEST', NOW(), 'TEST', NOW())</insert><delete id="updateBoard" parameterType="com.api.board.domain.Board">UPDATE BOARD.TB_BOARDSET BOARD_SUBJECT = #{board_subject}, BOARD_CONTENT = #{board_content}, UPD_USER_ID = 'TEST', UPD_DATE = NOW()WHERE BOARD_SEQ = #{board_seq}</delete><delete id="deleteBoard" parameterType="int">DELETEFROM BOARD.TB_BOARDWHERE BOARD_SEQ = #{board_seq}</delete></mapper>cs BoardMapper.java
더보기1234567891011121314151617181920212223package com.api.board.mapper;import java.util.List;import com.api.board.domain.Board;public interface BoardMapper {/** 게시글 목록 조회 */public List<Board> getBoardList() throws Exception;/** 게시글 상세 조회 */public Board getBoardDetail(int board_seq) throws Exception;/** 게시글 등록 */public int insertBoard(Board board) throws Exception;/** 게시글 수정 */public int updateBoard(Board board) throws Exception;/** 게시글 삭제 */public int deleteBoard(int board_seq) throws Exception;}cs 2. JUnit Test Case 작성
구현한 Mapper가 정상적으로 작동하는지 테스트하기 위해 BoardMapper 클래스의 테스크 케이스를 작성하겠습니다.
테스트 코드는 src/test/java 폴더 하위에 작성하며, 일반적으로는 테스트 패키지를 테스트 대상이 되는 패키지와 동일한 이름으로 작성합니다. 테스트 클래스명은 테스트 대상 클래스명에 접미사로 "Test"를 붙이고 테스트할 메소드에는 @Test 어노테이션을 추가하세요.
2_1. BoardMapper 선택 후 우클릭 > New > JUnit Test Case 선택
2_2. JUnit4 선택
2_3. BoadMapper에서 테스트할 메소드를 선택
2_4. 생성된 BoardMapperTest 확인
BoardMaperTest.java
더보기123456789101112131415161718192021222324252627282930313233package com.api.board.mapper;import static org.junit.Assert.*;import org.junit.Test;public class BoardMapperTest {@Testpublic void testGetBoardList() {fail("Not yet implemented");}@Testpublic void testGetBoardDetail() {fail("Not yet implemented");}@Testpublic void testInsertBoard() {fail("Not yet implemented");}@Testpublic void testUpdateBoard() {fail("Not yet implemented");}@Testpublic void testDeleteBoard() {fail("Not yet implemented");}}cs 3. Mapper 테스트
Spring Boot 2.4.x에서 JUnit4를 사용하려면 Vintage Engine 모듈을 제외해야 하기 때문에 다음과 같이 설정을 추가하세요.
pom.xml
더보기12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.8</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.api</groupId><artifactId>board</artifactId><version>0.0.1-SNAPSHOT</version><name>board</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 --><dependency><groupId>org.bgee.log4jdbc-log4j2</groupId><artifactId>log4jdbc-log4j2-jdbc4.1</artifactId><version>1.16</version></dependency><!-- JUnit4 사용하기 위해서 Vintage Engine 모듈을 제외 --><dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>cs @Transactional 어노테이션을 선언하면 테스트 메소드가 끝날 때마다 transaction을 롤백합니다. 테스트 메소드에 @Rollback 어노테이션을 추가해서 메소드 별로 Rollback 여부를 설정할 수 있습니다.
BoardMaperTest.java
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124package com.api.board.mapper;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertTrue;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.boot.test.context.SpringBootTest;import org.springframework.test.annotation.Rollback;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Transactional;import com.api.board.domain.Board;@Transactional@RunWith(SpringRunner.class)@SpringBootTestpublic class BoardMapperTest {Logger logger = LoggerFactory.getLogger(BoardMapperTest.class);@AutowiredBoardMapper boardMapper;int boardSeq = 0;/** 게시글 목록 조회 시 NULL 아니면 테스트 통과 */@Testpublic void testGetBoardList() {try {List<Board> boardList = boardMapper.getBoardList();assertNotNull(boardList);} catch (Exception e) {e.printStackTrace();}}/** 게시글 상세 조회 시 NULL이 아니면 테스트 통과 */@Testpublic void testGetBoardDetail() {try {if(boardSeq != 0) {Board boardDetail = boardMapper.getBoardDetail(boardSeq);assertNotNull(boardDetail);}} catch (Exception e) {e.printStackTrace();}}/** 게시글 등록 후 1이 응답되면 테스트 통과 */@Rollback(true)@Testpublic void testInsertBoard() {try {Board board = new Board();board.setBoard_writer("게시글 작성자 등록");board.setBoard_subject("게시글 제목 등록");board.setBoard_content("게시글 내용 등록");int result = boardMapper.insertBoard(board);assertTrue(result == 1);} catch (Exception e) {e.printStackTrace();}}/** 게시글 수정 후 1이 응답되면 테스트 통과 */@Rollback(true)@Testpublic void testUpdateBoard() {try {if(boardSeq != 0) {Board board = new Board();board.setBoard_seq(boardSeq);board.setBoard_subject("게시글 제목 수정");board.setBoard_content("게시글 내용 수정");int result = boardMapper.updateBoard(board);assertTrue(result == 1);}} catch (Exception e) {e.printStackTrace();}}/** 게시글 삭제 후 1이 응답되면 테스트 통과 */@Rollback(true)@Testpublic void testDeleteBoard() {try {if(boardSeq != 0) {int result = boardMapper.deleteBoard(boardSeq);assertTrue(result == 1);}} catch (Exception e) {e.printStackTrace();}}}cs 소스 코드는 Github Repository - https://github.com/tychejin1218/api-board_v1 (branch : section05) 를 참조하세요.
Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33반응형'Spring Boot > 2.4.x - REST API 만들기' 카테고리의 다른 글
[Spring Boot] REST API 만들기(7) - Transaction 적용 (0) 2020.05.09 [Spring Boot] REST API 만들기(6) - Interceptor 적용 (1) 2020.05.07 [Spring Boot] REST API 만들기(4) - Log4jdbc 설정 (0) 2020.05.05 [Spring Boot] REST API 만들기(3) - Logback 설정 (0) 2020.05.04 [Spring Boot] REST API 만들기(2) - MySQL + MyBatis 연동 (0) 2020.05.03