Junit

[Spring] Junit 단위 테스트
Feb 21, 2024
Junit
  1. 테스트를 하려면 모듈화가 되어야함. → 책임을 분리하기 위해
  1. 테스트는 격리되어 진행되어야 한다. → 가짜 데이터들을 넣어 테스트
  1. 테스트는 완벽하게 모든 예외들을 테스트하여야 한다. → 하지만 완벽할 수는 없다.
  1. 최종적으로 통합테스트를 진행한다.
    1. 유기적으로 연결된 모듈의 오류를 잡을 수 있음.

Git에서 다운로드

git log 확인해서 setting으로 reset

BoardRopository

package shop.mtcoding.blog.board; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import java.util.List; @RequiredArgsConstructor @Repository public class BoardRepository { private final EntityManager em; @Transactional public void delete(int id) { Query query = em.createNativeQuery("delete from board_tb where id = ?"); query.setParameter(1,id); query.executeUpdate(); } @Transactional public void update(String title, String content, String author, int id) { Query query = em.createNativeQuery("update board_tb set title=?, content=?, author=? where id = ?"); query.setParameter(1,title); query.setParameter(2,content); query.setParameter(3,author); query.setParameter(4,id); query.executeUpdate(); } public List<Board> selectAll() { Query query = em.createNativeQuery("select * from board_tb ", Board.class); List<Board> boardList = (List<Board>) query.getResultList(); // 못찾으면 빈 컬렉션을 준다 (크기 = 0) return boardList; } public Board selectOne(int id) { Query query = em.createNativeQuery("select * from board_tb where id = ?", Board.class); query.setParameter(1, id); try { Board board = (Board) query.getSingleResult(); return board; } catch (Exception e) { return null; } } @Transactional public void insert(String title, String content, String author) { Query query = em.createNativeQuery("insert into board_tb(title, content, author) values(?, ?, ?)"); query.setParameter(1, title); query.setParameter(2, content); query.setParameter(3, author); query.executeUpdate(); } }

BoardRepositoryTest

package shop.mtcoding.blog.board; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; import java.util.List; @Import(BoardRepository.class) // 내가 만든 클래스는 import 해줘야 함. @DataJpaTest // DB 관련 객체들이 IoC에 뜬다. public class BoardRepositoryTest { @Autowired // Test에서 DI 하는 코드 private BoardRepository boardRepository; @Test public void delete(){ // given int id = 1; // when boardRepository.delete(id); // then List<Board> boardList = boardRepository.selectAll(); Assertions.assertThat(boardList.size()).isEqualTo(7); } @Test public void update(){ // given String title = "제목10"; String content = "내용10"; String author = "이순신"; int id = 1; // when boardRepository.update(title,content,author,id); // then Board board = boardRepository.selectOne(id); // System.out.println(board); Assertions.assertThat(board.getTitle()).isEqualTo(title); Assertions.assertThat(board.getContent()).isEqualTo(content); Assertions.assertThat(board.getAuthor()).isEqualTo(author); } @Test public void selectAll_test(){ // given // when List<Board> boardList = boardRepository.selectAll(); // then (상태 검사) // System.out.println(boardList); Assertions.assertThat(boardList.get(0).getTitle()).isEqualTo("제목1"); Assertions.assertThat(boardList.get(0).getContent()).isEqualTo("내용1"); Assertions.assertThat(boardList.get(0).getAuthor()).isEqualTo("홍길동"); Assertions.assertThat(boardList.size()).isEqualTo(8); } @Test public void selectOne_test(){ // given int id = 1; // when Board board = boardRepository.selectOne(id); // then (상태 검사) // System.out.println(board); Assertions.assertThat(board.getTitle()).isEqualTo("제목1"); Assertions.assertThat(board.getContent()).isEqualTo("내용1"); Assertions.assertThat(board.getAuthor()).isEqualTo("홍길동"); } @Test public void insert_test(){ // 테스트 메서드는 파라미터가 없다. 리턴도 없다. // given String title = "제목10"; String content = "내용10"; String author = "이순신"; // when boardRepository.insert(title, content, author); // then -> 눈으로 확인 (쿼리) } // Rollback (자동) }

Assertion

notion image
2개중에 assertj 쓰기

틀린 값도 넣어서 Test 해보기

notion image
notion image
 
Share article
RSSPowered by inblog