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()); + } +}