Spring 토이프로젝트 Entity , Repository 간단 Test 코드
간단 repository를 통해 entity 제약조건 테스트
- 아래 코드와 같은 방식으로 entity builder에서 널처리를 테스트하기 위해 테스트 코드를 작성했습니다.
- 객체 테스트이기 때문에 @SpringBootTest나 @DataJpaTest등은 사용하지 않았습니다.
- 다른 코드의 테스트도 동일합니다.
@DataJpaTest
class BoardRepositoryTest {
User user;
String content;
String title;
@Autowired
BoardRepository boardRepository;
@Autowired
EntityManager em;
@BeforeEach
void beforeEach() {
user = user.builder()
.username("test")
.email("test@test.com")
.password("test")
.build();
content = "content";
title = "title";
}
@Test
void 제목최대길이실패테스트(){
title = "testtesttesttesttesttesttesttesttesttesttesttesttesttesttest";
Board board = Board.builder()
.title(title)
.content(content)
.user(user)
.build();
assertThrows(DataIntegrityViolationException.class, () -> {
boardRepository.save(board);
});
}
@Test
void 생성_성공테스트() {
Board board = Board.builder()
.title(title)
.content(content)
.user(user)
.build();
boardRepository.save(board);
}
}
Entity Test
public class BoardTest {
User user;
String content;
String title;
@BeforeEach
void beforeEach() {
user = user.builder()
.username("test")
.email("test@test.com")
.password("test")
.build();
content = "content";
title = "title";
}
@Test
void 게시판생성성공테스트() {
Board board = Board.builder()
.content(content)
.title(title)
.user(user)
.build();
assertThat(board.getContent()).isEqualTo(content);
assertThat(board.getTitle()).isEqualTo(title);
assertThat(board.getUser()).isEqualTo(user);
}
@Test
void 게시판제목없은실패테스트() {
title = "";
assertThrows(IllegalArgumentException.class, () ->
{
Board board = Board.builder()
.content(content)
.title(title)
.user(user)
.build();
});
assertThrows(IllegalArgumentException.class, () ->
{
Board board = Board.builder()
.content(content)
.title(null)
.user(user)
.build();
});
}
@Test
void 게시판내용없음실패테스트() {
content = "";
assertThrows(IllegalArgumentException.class, () ->
{
Board board = Board.builder()
.content(content)
.title(title)
.user(user)
.build();
});
assertThrows(IllegalArgumentException.class, () ->
{
Board board = Board.builder()
.content(null)
.title(title)
.user(user)
.build();
});
}
@Test
void 게시판유저없음실패테스트() {
content = "";
assertThrows(IllegalArgumentException.class, () ->
{
Board board = Board.builder()
.content(content)
.title(title)
.user(user)
.build();
});
assertThrows(IllegalArgumentException.class, () ->
{
Board board = Board.builder()
.content(content)
.title(title)
.user(null)
.build();
});
}
}
Auditing Test
- Jpa의 기능인 Auditing을 테스트 합니다.
- Auditing 세팅을 가져오기 위해서는 JpaConfig를 가져와야 하기 때문에 @DataJpaTest로만은 구현이 어렵습니다.
- 일단은 @SpringBootTest를 통해 진행 한 뒤 후에 @DataJpaTest에 추가 하는 방식으로 변경하겠습니다.
- Jpa는 기본적으로 transaction으로 동작합니다.
@SpringBootTest
@Transactional
public class AuditingTest {
@Autowired
UserRepository userRepository;
@Autowired
BoardRepository boardRepository;
@Autowired
CommentRepository commentRepository;
@Autowired
EntityManager em;
@Test
void 유저_Auditing() {
User user = User.builder()
.username("test")
.email("test@email.com")
.password("test")
.build();
User saveUser = userRepository.save(user);
assertThat(saveUser.getCreatedDate()).isNotNull();
assertThat(saveUser.getModifiedDate()).isNotNull();
}
@Test
void 게시판_Auditing() {
User user = User.builder()
.username("test")
.email("test@email.com")
.password("test")
.build();
Board board = Board.builder()
.content("test")
.title("title")
.user(user)
.build();
User saveUser = userRepository.save(user);
Board saveBoard = boardRepository.save(board);
assertThat(saveBoard.getCreatedDate()).isNotNull();
assertThat(saveBoard.getModifiedDate()).isNotNull();
assertThat(saveBoard.getCreatedBy()).isNotNull();
assertThat(saveBoard.getUpdatedBy()).isNotNull();
}
@Test
void comment_Auditing() {
User user = User.builder()
.username("test")
.email("test@email.com")
.password("test")
.build();
Board board = Board.builder()
.content("test")
.title("title")
.user(user)
.build();
Comment comment = Comment.builder()
.board(board)
.user(user)
.content("test")
.build();
User saveUser = userRepository.save(user);
Board saveBoard = boardRepository.save(board);
Comment saveComment = commentRepository.save(comment);
assertThat(saveComment.getCreatedDate()).isNotNull();
assertThat(saveComment.getModifiedDate()).isNotNull();
assertThat(saveComment.getCreatedBy()).isNotNull();
assertThat(saveComment.getUpdatedBy()).isNotNull();
}
@Test
void merge테스트() {
User user = User.builder()
.username("test")
.email("test@email.com")
.password("test")
.build();
Board board = Board.builder()
.title("test")
.content("hoiu")
.user(user)
.build();
userRepository.save(user);
Board saveBoard = boardRepository.save(board);
em.flush();
em.clear();
Board find = boardRepository.findById(saveBoard.getId()).get();
find.setContent("오잉이잉");
System.out.println(em.contains(find));
Board merge = em.merge(find);
em.flush();
em.clear();
System.out.println(em.contains(find));
find.setContent("오잉");
em.flush();
em.clear();
}
}