application.yml의 ddl-auto를 잠시 none으로 변경
Mac 기준 option + enter 를 누르고 Create Test 클릭
JUnit5로 설정하고 OK
BoardRepositoryTest.java
package jpa.board.repository;
import jpa.board.dto.BoardDto;
import jpa.board.entity.Board;
import jpa.board.entity.Member;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
class BoardRepositoryTest {
@Autowired
BoardRepository boardRepository;
@Autowired
MemberRepository memberRepository;
@Test
public void 게시판_등록() {
//등록된 글 다 지우기
boardRepository.deleteAll();
//given
String title = "타이틀1";
String content = "내용1";
List<Member> memberList = memberRepository.findAll();
Member member = memberList.get(0);
BoardDto boardDto = new BoardDto(title, content);
Board board = boardDto.toEntity(member);
boardRepository.save(board);
//when
List<Board> boardList = boardRepository.findAll();
//then
Board boards = boardList.get(0);
assertThat(boards.getTitle()).isEqualTo(title);
assertThat(boards.getContent()).isEqualTo(content);
}
@Test
public void 게시판_수정() {
//given
List<Board> boardList = boardRepository.findAll();
Board boards = boardList.get(0);
String title = "변경된 타이틀";
String content = "변경된 내용";
boards.update(title, content);
boardRepository.save(boards);
//when
List<Board> boardList2 = boardRepository.findAll();
//then
Board boards2 = boardList2.get(0);
assertThat(boards2.getTitle()).isEqualTo(title);
assertThat(boards2.getContent()).isEqualTo(content);
}
}
BoardDto.java
package jpa.board.dto;
import jpa.board.entity.Board;
import jpa.board.entity.Member;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class BoardDto {
private Long id; //시퀀스
private String title; //제목
private String content; //내용
private LocalDateTime regDate; //등록 날짜
private LocalDateTime uptDate; //수정 날짜
private Long viewCount; //조회수
private String username; //사용자 이름
public BoardDto(String title, String content){
this.title = title;
this.content = content;
}
public Board toEntity(Member member){
return Board.builder()
.member(member)
.title(title)
.content(content)
.build();
}
}
Test에 필요한 BoardDto를 약식으로 생성
Title과 Content를 보면 실행이 잘 된걸 확인 가능
Title과 Content를 보면 실행이 잘 된걸 확인 가능
유익한 정보를 얻을 수 있어서 기쁩니다.