ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Boot] JPA 게시판 만들기(2) - API 테스트 코드 구현
    Spring Boot/2.3.x - JPA 게시판 만들기 2020. 7. 20. 15:19
    반응형

    @SpringBootTest와 TestRestTemplate를 사용하여 게시글을 조회, 등록, 수정, 삭제하는 API 테스트하는 코드를 구현하겠습니다.


    BoardControllerTest.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
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    package com.rest.api.board.controller;
     
    import static org.assertj.core.api.Assertions.assertThat;
     
    import java.util.List;
     
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.web.client.TestRestTemplate;
    import org.springframework.boot.web.server.LocalServerPort;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.test.context.junit4.SpringRunner;
     
    import com.rest.api.board.domain.Board;
    import com.rest.api.board.dto.BoardSaveRequestDto;
    import com.rest.api.board.dto.BoardUpdateRequestDto;
    import com.rest.api.board.repository.BoardRepository;
     
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class BoardControllerTest {
     
        @LocalServerPort
        private int port;
     
        @Autowired
        private TestRestTemplate restTemplate;
     
        @Autowired
        private BoardRepository boardRepository;
     
        @Test
        public void findAll() throws Exception {
     
            String url = "http://localhost:" + port + "/api/v1/board";
     
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
     
            assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
            assertThat(responseEntity.getBody()).contains("boardSeq");
            assertThat(responseEntity.getBody()).contains("boardSubject");
            assertThat(responseEntity.getBody()).contains("boardWriter");
            assertThat(responseEntity.getBody()).contains("boardContent");
        }
     
        @Test
        public void findById() throws Exception {
     
            Long boardSeq = 1L;
     
            String url = "http://localhost:" + port + "/api/v1/board/" + boardSeq;
     
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
     
            assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
            assertThat(responseEntity.getBody()).contains("boardSeq");
            assertThat(responseEntity.getBody()).contains("boardSubject");
            assertThat(responseEntity.getBody()).contains("boardWriter");
            assertThat(responseEntity.getBody()).contains("boardContent");
        }
     
        @Test
        public void save() throws Exception {
     
            String boardSubject = "게시글_제목_저장_01";
            String boardWriter = "게시글_작성자_저장_01";
            String boardContent = "게시글_내용_저장_01";
     
            BoardSaveRequestDto boardSaveRequestDto = BoardSaveRequestDto.builder()
                                                                         .boardSubject(boardSubject)
                                                                         .boardWriter(boardWriter)
                                                                         .boardContent(boardContent)
                                                                         .build();
     
            String url = "http://localhost:" + port + "/api/v1/board";
     
            ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, boardSaveRequestDto, Long.class);
     
            assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.CREATED);
            assertThat(responseEntity.getBody()).isGreaterThan(0L);
     
            List<Board> boardList = boardRepository.findAll();
            Board board = boardList.get(boardList.size() - 1);
            assertThat(board.getBoardSubject()).isEqualTo(boardSubject);
            assertThat(board.getBoardWriter()).isEqualTo(boardWriter);
            assertThat(board.getBoardContent()).isEqualTo(boardContent);
        }
     
        @Test
        public void update() throws Exception {
     
            String boardSubject = "게시글_제목_저장_01";
            String boardWriter = "게시글_작성자_저장_01";
            String boardContent = "게시글_내용_저장_01";
     
            Board saveBoard = boardRepository.save(Board.builder()
                                                        .boardSubject(boardSubject)
                                                        .boardWriter(boardWriter)
                                                        .boardContent(boardContent)
                                                        .build());
     
            Long boardSeq = saveBoard.getBoardSeq();
            String updatedBoardSubject = "게시글_제목_수정_01";
            String updatedBoardContent = "게시글_내용_수정_01";
     
            BoardUpdateRequestDto boardUpdateRequestDto = BoardUpdateRequestDto.builder()
                                                                               .boardSubject(updatedBoardSubject)
                                                                               .boardContent(updatedBoardContent)
                                                                               .build();
     
            String url = "http://localhost:" + port + "/api/v1/board/" + boardSeq;
     
            HttpEntity<BoardUpdateRequestDto> requestEntity = new HttpEntity<>(boardUpdateRequestDto);
     
            ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Long.class);
     
            assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.CREATED);
            assertThat(responseEntity.getBody()).isGreaterThan(0L);
     
            List<Board> boardList = boardRepository.findAll();
            Board board = boardList.get(boardList.size() - 1);
            assertThat(board.getBoardSubject()).isEqualTo(updatedBoardSubject);
            assertThat(board.getBoardContent()).isEqualTo(updatedBoardContent);
        }
     
        @Test
        public void delete() throws Exception {
     
            String boardSubject = "게시글_제목_저장_01";
            String boardWriter = "게시글_작성자_저장_01";
            String boardContent = "게시글_내용_저장_01";
     
            Board saveBoard = boardRepository.save(Board.builder()
                                                        .boardSubject(boardSubject)
                                                        .boardWriter(boardWriter)
                                                        .boardContent(boardContent)
                                                        .build());
     
            Long boardSeq = saveBoard.getBoardSeq();
     
            String url = "http://localhost:" + port + "/api/v1/board/" + boardSeq;
     
            HttpHeaders httpHeaders = new HttpHeaders();
            HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);
     
            ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, Long.class);
     
            assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
        }
    }
    cs

    Github Repository - 

    https://github.com/tychejin1218/jpa-sample


    반응형

    댓글

Designed by Tistory.