개발 무지렁이

[Spring Boot] 리포지터리 CRUD 메서드 테스트 본문

Backend/스프링부트

[Spring Boot] 리포지터리 CRUD 메서드 테스트

Gaejirang-e 2022. 11. 25. 14:56

리포지터리


(엔티티에 의해 생성된) 데이터베이스 테이블에 접근하는 'CRUD 메서드'들을 사용하기 위한 인터페이스를 말한다.
public interface QuestionRepository extends JpaRepository<Question, Integer> {
    // 리포지터리로 만들기 위해 JpaRepository 인터페이스를 상속
    // < 대상이 되는 엔티티타입, PK 속성 타입 >
}
  • Create

    • save()

      @SpringBootTest
      class App59ApplicationTests {
        @Autowired // 객체 주입
        private QuestionRepository questionRepository;
      
        @Test
        void testJpa() {
            Question q = new Question();
            q.setSubject("안녕하세요");
            q.setContent("가입인사드립니다^^");
            q.setCreateDate(LocalDateTime.now());
            this.questionRepository.save(q);
        }
      }
  • Read

    • findAll()

      @SpringBootTest
      class App59ApplicationTests {
        @Autowired
        private QuestionRepository questionRepository;
      
        @Test
        void testJpa() {
            List<Question> all = this.questionRepository.findAll();
            assertEquals(1, all.size()); // JUnit, (기대값, 실제값)
        }
      }
    • findById()

      @SpringBootTest
      class App59ApplicationTests {
        @Autowired
        private QuestionRepository questionRepository;
      
        @Test
        void testJpa() {
            Optional<Question> oq = this.questionRepository.findById(1); // 리턴타입 Optional<>
            if(oq.isPresent()) {
                Question q = oq.get();
                assertEquals("안녕하세요.", q.getSubject());
            }
        }
      }

      ❓Optional
      : null 처리를 유연하기 하기 위해 사용하는 클래스 (간편한 기능이 섞인 포장지)

    • findBySubject() (findBy대상엔티티속성())

      public interface QuestionRepository extends JpaRepository<Question, Integer> {
        Question findBySubject(String subject);
      }
      @SpringBootTest
      class App59ApplicationTests {
        @Autowired
        private QuestionRepository questionRepository;
      
        @Test
        void testJpa() {
            Question q = this.questionRepository.findBySubject("안녕하세요.");
            assertEquals(1, q.getId());
        }
      }
    • findBySubjectAndContent() (두개의 속성을 And 조건으로 조회)

      public interface QuestionRepository extends JpaRepository<Question, Integer> {
        Question findBySubject(String subject);
        Question findBySubjectAndContent(String subject, String content);
      }
      @SpringBootTest
      class App59ApplicationTests {
        @Autowired
        private QuestionRepository questionRepository;
      
        @Test
        void testJpa() {
            Question q = this.questionRepository.findBySubjectAndContent("안녕하세요.", "가입인사드립니다.^^"); 
            assertEquals(1, q.getId());
        }
      }

      ※ Or / Between / Like / In / OrderBy

  • Update

      @SpringBootTest
      class App59ApplicationTests {
          @Autowired
          private QuestionRepository questionRepository;
    
          @Test
          void testJpa() {
              Optional<Question> oq = this.questionRepository.findById(1); 
              if(oq.isPresent()) {
                  Question q = oq.get();
                  q.setSubject("반갑습니다.");
                  this.questionRepository.save(q);
              }
          }
      }
  • Delete

    • delete()

      @SpringBootTest
      class App59ApplicationTests {
        @Autowired
        private QuestionRepository questionRepository;
      
        @Test
        void testJpa() {
            assertEquals(1, this.questionRepository.count());
            Optional<Question> oq = this.questionRepository.findById(1); 
            if(oq.isPresent()) {
                Question q = oq.get();
                this.questionRepository.delete(q);
                assertEquals(0, this.questionRepository.count());
            }
        }
      }
Comments