-
[Spring Boot] JPA 게시판 만들기(2) - API 테스트 코드 구현Spring Boot/2.3.x - JPA 게시판 만들기 2020. 7. 20. 15:19반응형
@SpringBootTest와 TestRestTemplate를 사용하여 게시글을 조회, 등록, 수정, 삭제하는 API 테스트하는 코드를 구현하겠습니다.
BoardControllerTest.java
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156package 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 {@LocalServerPortprivate int port;@Autowiredprivate TestRestTemplate restTemplate;@Autowiredprivate BoardRepository boardRepository;@Testpublic 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");}@Testpublic 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");}@Testpublic 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);}@Testpublic 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);}@Testpublic 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반응형'Spring Boot > 2.3.x - JPA 게시판 만들기' 카테고리의 다른 글
[Spring Boot] JPA 게시판 만들기(3) - JPA Auditing 설정 (0) 2020.07.20 [Spring Boot] JPA 게시판 만들기(1) - 기본 설정 및 CRUD 구현 (0) 2020.06.25