테스트 코드 작성하기

온라인 도서관 프로젝트에서 기능을 하나씩 확장할 때마다, 기능을 추가 하고 나서 천천히 사이트를 살펴볼 때, 갑자기 다른 곳 기능이 작동하지 않는 문제가 종종 발생해서 스트레스를 받았다. 그래서 전체적으로 모든 도메인 계층의 서비스 코드를 작성했다

테스트 코드

이 코드는 검색 조건이 없을 경우, 있을 경우를 모두 테스트하는 코드이다.

@Transactional
@SpringBootTest
class BookRepositoryTest {

...

    @Test
    void findAll_no_cond() {
        //given
        for (int i = 0; i < 3; i++) {
            Book book = new Book("test" + i, "testAuthor" + i, "12345" + i);
            bookRepository.save(book);
        }
        //when
        Pageable pageable = PageRequest.of(0, 2);
        BookSearchCond cond = new BookSearchCond();
        List<Book> books = bookRepository.findAll(cond, pageable.getPageSize(), pageable.getOffset());

        //then
        assertThat(books.size()).isEqualTo(2);
    }


    @Test
    void findAll_cond_bookName() {
        //given
        for (int i = 0; i < 20; i++) {
            Book book = new Book("test" + i, "testAuthor" + i, "1234" + i);
            bookRepository.save(book);
        }

        for (int i = 0; i < 2; i++) {
            Book book = new Book( "newBook" + i, "testAuthor1", "5678" + i);
            bookRepository.save(book);
        }

        //when
        Pageable pageable = PageRequest.of(0, 10);
        BookSearchCond cond = new BookSearchCond("newBook", null, null);
        List<Book> books = bookRepository.findAll(cond, pageable.getPageSize(), pageable.getOffset());

        //then
        assertThat(books.size()).isEqualTo(2);
    }


    @Test
    void findAll_cond_author() {
        //given
        for (int i = 0; i < 20; i++) {
            Book book = new Book("test" + i, "testAuthor" + i, "1234" + i);
            bookRepository.save(book);
        }

        for (int i = 0; i < 3; i++) {
            Book book = new Book( "newBook" + i, "김철준" + i, "5678" + i);
            bookRepository.save(book);
        }

        //when
        Pageable pageable = PageRequest.of(0, 10);
        BookSearchCond cond = new BookSearchCond(null, "김철준", null);
        List<Book> books = bookRepository.findAll(cond, pageable.getPageSize(), pageable.getOffset());

        //then
        assertThat(books.size()).isEqualTo(3);
    }
    @Test
    void findAll_cond_isbn() {
        //given
        for (int i = 0; i < 20; i++) {
            Book book = new Book("test" + i, "testAuthor" + i, "1234" + i);
            bookRepository.save(book);
        }

        Book findBook = new Book("unique book", "김철준", "94932");
        bookRepository.save(findBook);

        //when
        Pageable pageable = PageRequest.of(0, 10);
        BookSearchCond cond = new BookSearchCond(null, null, "94932");
        List<Book> books = bookRepository.findAll(cond, pageable.getPageSize(), pageable.getOffset());

        //then
        assertThat(books.size()).isEqualTo(1);
        assertThat(books.get(0).getBookName()).isEqualTo("unique book");
    }

    @Test
    void findAll_cond_and() {
        //given
        for (int i = 0; i < 20; i++) {
            Book book = new Book("test" + i, "testAuthor" + i, "1234" + i);
            bookRepository.save(book);
        }

        Book findBook = new Book("unique book", "김철준", "94932");
        bookRepository.save(findBook);

        Book likeBook = new Book("not unique book", "김철준 유사 작가", "949324");
        bookRepository.save(likeBook);

        //when
        Pageable pageable = PageRequest.of(0, 10);
        BookSearchCond cond = new BookSearchCond("unique book", "김철준", "94932");
        List<Book> books = bookRepository.findAll(cond, pageable.getPageSize(), pageable.getOffset());

        //then
        assertThat(books.size()).isEqualTo(1);
        assertThat(books.get(0).getBookName()).isEqualTo("unique book");
    }

...

}

후기

테스트코드를 전체적으로 작성하면 작성할 때는 기능 하나 추가하는데 이렇게 까지 코드를 더 써야 할까? 라는 의문은 들지만, 나중에 보면 기능 추가 후 다른 곳에서 사이드 이펙트가 발생하지 않는지를 즉각 체크할 수 있어서 오히려 편리하다.

그리고 테스트 코드를 쭉 돌리고 파란 체크가 들어온 것을 보면 꽤 기분이 좋기도 하다!

댓글

개발자  김철준

백엔드 개발자 김철준의 블로그입니다.

주요 프로젝트