Skip to content

Commit

Permalink
test : 공지사항 조회 시 정렬 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokho-1116 committed Jul 25, 2024
1 parent 61cff96 commit 787faf3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<AnnouncementDto> announcementDtos(int size) {
List<AnnouncementDto> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<AnnouncementDto> announcements = announcementService.findAll();

//then
assertThat(announcements)
.isSortedAccordingTo(Comparator.comparing(AnnouncementDto::createdAt).reversed());
}
}

0 comments on commit 787faf3

Please sign in to comment.