프로젝트

16일차

사랑박 2023. 3. 7. 12:29

오늘은 Part2.게시판 서비스 강의의 `데이터베이스 접근 로직 구현` 부분을 학습하였다.
이전에 DB 접근 방법을 세팅하고 테스트 만든 내용을 토대로, 남은 구현이 있는지 확인하고 마무리 하는 과정이다.
 

테스트 데이터 생성하는 사이트

아래 사이트에서 테스트 데이터를 생성하여 프로젝트에서 사용하였다.
https://www.mockaroo.com/ 

 

Mockaroo - Random Data Generator and API Mocking Tool | JSON / CSV / SQL / Excel

Mock your back-end API and start coding your UI today. It's hard to put together a meaningful UI prototype without making real requests to an API. By making real requests, you'll uncover problems with application flow, timing, and API design early, improvi

www.mockaroo.com

 

게시판 서비스 프로젝트 JPA 테스트 작성

기본 CRUD + 연관관계 매핑, CASCADING이 잘 동작하는지 확인하는 테스트를 작성하였다.
 
select 테스트

    @DisplayName("select 테스트")
    @Test
    void givenTestData_whenSelecting_thenWorksFine(){
        // Given

        // When
        List<Article> articles = articleRepository.findAll();

        //Then
        assertThat(articles).isNotNull().hasSize(123);
    }

insert 테스트

    @DisplayName("insert 테스트")
    @Test
    void givenTestData_whenInserting_thenWorksFine(){
        // Given
        long previousCount=articleRepository.count();


        // When
        Article article = articleRepository.save(Article.of("new article", "new content", "#spring"));

        //Then
        assertThat(articleRepository.count()).isEqualTo(previousCount+1);
    }

update 테스트

    @DisplayName("update 테스트")
    @Test
    void givenTestData_whenUpdating_thenWorksFine(){
        // Given
        Article article=articleRepository.findById(1L).orElseThrow();
        String updatedHashtag="#springboot";
        article.setHashtag(updatedHashtag);

        // When
        Article savedArticle=articleRepository.save(article);

        //Then
        assertThat(savedArticle).hasFieldOrPropertyWithValue("hashtag",updatedHashtag);
    }

delete 테스트

    @DisplayName("delete 테스트")
    @Test
    void givenTestData_whenDeleting_thenWorksFine(){
        // Given
        Article article=articleRepository.findById(1L).orElseThrow();
        long previousArticleCount=articleRepository.count();
        long previousArticleCommentCount=articleCommentRepository.count();
        int deletedCommentSize=article.getArticleComments().size();

        // When
        articleRepository.delete(article);

        //Then
        assertThat(articleRepository.count()).isEqualTo(previousArticleCount-1);
        assertThat(articleCommentRepository.count()).isEqualTo(previousArticleCommentCount-deletedCommentSize);
    }

 
@DataJpaTest

  • JPA에 관련된 요소들만 테스트하기 위한 어노테이션으로 JPA 테스트에 관련된 설정들만 적용
  • 메모리상에 내부 데이터베이스를 생성하고 @Entity 클래스들을 등록하고 JPA Repository 설정들을 해준다. 각 테스트마다 테스트가 완료되면 관련한 설정들은 롤백

 
 

'프로젝트' 카테고리의 다른 글

18일차  (0) 2023.03.09
17일차  (0) 2023.03.08
15일차  (0) 2023.03.06
14일차  (0) 2023.03.05
13일차  (0) 2023.03.04