From 787faf33b7239b840bf5b61993ad7fd3b84d6c3a Mon Sep 17 00:00:00 2001 From: hong seokho Date: Thu, 25 Jul 2024 20:59:39 +0900 Subject: [PATCH] =?UTF-8?q?test=20:=20=EA=B3=B5=EC=A7=80=EC=82=AC=ED=95=AD?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EC=A0=95=EB=A0=AC=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/AnnouncementTestFixture.java | 22 +++++++++++++ .../service/AnnouncementServiceTest.java | 33 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/AnnouncementTestFixture.java create mode 100644 backend/core/src/test/java/site/timecapsulearchive/core/domain/announcement/service/AnnouncementServiceTest.java diff --git a/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/AnnouncementTestFixture.java b/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/AnnouncementTestFixture.java new file mode 100644 index 000000000..84877598a --- /dev/null +++ b/backend/core/src/test/java/site/timecapsulearchive/core/common/fixture/domain/AnnouncementTestFixture.java @@ -0,0 +1,22 @@ +package site.timecapsulearchive.core.common.fixture.domain; + +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; +import site.timecapsulearchive.core.domain.announcement.data.dto.AnnouncementDto; + +public class AnnouncementTestFixture { + + public static List announcementDtos(int size) { + List result = new ArrayList<>(); + ZonedDateTime now = ZonedDateTime.now(); + for (int index = 0; index < size; index++) { + result.add( + new AnnouncementDto("title" + index, "content" + index, String.valueOf(index), + now.plusSeconds(index)) + ); + } + + return result; + } +} diff --git a/backend/core/src/test/java/site/timecapsulearchive/core/domain/announcement/service/AnnouncementServiceTest.java b/backend/core/src/test/java/site/timecapsulearchive/core/domain/announcement/service/AnnouncementServiceTest.java new file mode 100644 index 000000000..090578723 --- /dev/null +++ b/backend/core/src/test/java/site/timecapsulearchive/core/domain/announcement/service/AnnouncementServiceTest.java @@ -0,0 +1,33 @@ +package site.timecapsulearchive.core.domain.announcement.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +import java.util.Comparator; +import java.util.List; +import org.junit.jupiter.api.Test; +import site.timecapsulearchive.core.common.fixture.domain.AnnouncementTestFixture; +import site.timecapsulearchive.core.domain.announcement.data.dto.AnnouncementDto; +import site.timecapsulearchive.core.domain.announcement.repository.AnnouncementRepository; + +class AnnouncementServiceTest { + + private final AnnouncementRepository announcementRepository = mock( + AnnouncementRepository.class); + private final AnnouncementService announcementService = new AnnouncementService( + announcementRepository); + + @Test + void 공지사항을_조회하면_가장_최근에_생성된_공지사항이_최상단에_위치한다() { + //given + given(announcementRepository.findAll()).willReturn(AnnouncementTestFixture.announcementDtos(10)); + + //when + List announcements = announcementService.findAll(); + + //then + assertThat(announcements) + .isSortedAccordingTo(Comparator.comparing(AnnouncementDto::createdAt).reversed()); + } +}