ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper 
        PUBLIC "-//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_DATE  
                             FROM BOARD.TB_BOARD
                         ORDER 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_DATE  
                          FROM BOARD.TB_BOARD
                         WHERE 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>
            
            INSERT 
              INTO 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_BOARD 
               SET 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">
        
            DELETE 
              FROM BOARD.TB_BOARD 
             WHERE BOARD_SEQ = #{board_seq}
        
        </delete>
        
    </mapper>
    cs

     

    BoardMapper.java 

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package 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

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package com.api.board.mapper;
     
    import static org.junit.Assert.*;
     
    import org.junit.Test;
     
    public class BoardMapperTest {
     
        @Test
        public void testGetBoardList() {
            fail("Not yet implemented");
        }
     
        @Test
        public void testGetBoardDetail() {
            fail("Not yet implemented");
        }
     
        @Test
        public void testInsertBoard() {
            fail("Not yet implemented");
        }
     
        @Test
        public void testUpdateBoard() {
            fail("Not yet implemented");
        }
     
        @Test
        public void testDeleteBoard() {
            fail("Not yet implemented");
        }
    }
    cs

     

    3. Mapper 테스트

    Spring Boot 2.4.x에서 JUnit4를 사용하려면 Vintage Engine 모듈을 제외해야 하기 때문에 다음과 같이 설정을 추가하세요.

    pom.xml

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    <?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

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    package 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)
    @SpringBootTest
    public class BoardMapperTest {
     
        Logger logger = LoggerFactory.getLogger(BoardMapperTest.class);
     
        @Autowired
        BoardMapper boardMapper;
        
        int boardSeq = 0;
        
        /** 게시글 목록 조회 시 NULL 아니면 테스트 통과 */
        @Test
        public void testGetBoardList() {
            
            try {
                
                List<Board> boardList = boardMapper.getBoardList();
                assertNotNull(boardList);
                
            } catch (Exception e) {
                e.printStackTrace();
            }        
        }
     
        /** 게시글 상세 조회 시 NULL이 아니면 테스트 통과 */
        @Test
        public void testGetBoardDetail() {
     
            try {
                            
                if(boardSeq != 0) {        
                    
                    Board boardDetail = boardMapper.getBoardDetail(boardSeq);
                    assertNotNull(boardDetail);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        /** 게시글 등록 후 1이 응답되면 테스트 통과 */
        @Rollback(true)
        @Test
        public 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)
        @Test    
        public 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)
        @Test
        public 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

    반응형

    댓글

Designed by Tistory.