Skip to content

Commit

Permalink
feat: topic service -> facade 테스트 코드 리펙토링
Browse files Browse the repository at this point in the history
- describe context it 패턴 적용
- topicServiceTest -> topicFacadeTest로 변경
- facade pattern에 맞게 수정
  • Loading branch information
kimdozzi committed Jul 10, 2024
1 parent ff75d02 commit a4c47ef
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setup() {
}

@Nested
@DisplayName("save 메소드는")
@DisplayName("save 메서드는")
class Describe_save {

@BeforeEach
Expand All @@ -75,7 +75,7 @@ public void it_returns_the_saved_obj_if_saving_an_obj_succeeds() {


@Nested
@DisplayName("search 메소드는")
@DisplayName("search 메서드는")
class Describe_search {

@Nested
Expand Down Expand Up @@ -129,7 +129,7 @@ public void init() {

@Test
@DisplayName("생성된 인스턴스가 존재하면 description만 수정할 수 있고, 없다면 모든 항목을 수정할 수 있다.")
public void it_returns_2XX_if_the_obj_is_updated_successfully() {
public void it_returns_updated_obj() {

Topic topic = topicRepository.findById(topicA.getId()).orElse(null);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.genius.gitget.admin.topic.service;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.genius.gitget.global.util.exception.BusinessException;
import com.genius.gitget.topic.domain.Topic;
import com.genius.gitget.topic.dto.TopicCreateRequest;
import com.genius.gitget.topic.dto.TopicDetailResponse;
import com.genius.gitget.topic.dto.TopicUpdateRequest;
import com.genius.gitget.topic.serviceFacade.TopicFacade;
import jakarta.transaction.Transactional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;

@SpringBootTest
@Transactional
@Rollback
public class TopicFacadeTest {
Topic topicA, topicB;
String fileType;

@Autowired
TopicFacade topicFacade;

@BeforeEach
public void setup() {
topicA = Topic.builder()
.title("1일 1알고리즘")
.description("하루에 한 문제씩 문제를 해결합니다.")
.tags("BE, FE, CS")
.pointPerPerson(100)
.build();

topicB = Topic.builder()
.title("1일 2알고리즘")
.description("하루에 한 문제씩 문제를 해결합니다.")
.tags("BE, FE, CS")
.pointPerPerson(300)
.build();

fileType = "topic";
}

private TopicCreateRequest getTopicCreateRequest() {
return TopicCreateRequest.builder()
.title(topicA.getTitle())
.description(topicA.getDescription())
.tags(topicA.getTags())
.pointPerPerson(topicA.getPointPerPerson())
.notice(topicA.getNotice())
.build();
}

@Nested
@DisplayName("토픽 생성 메서드는")
class Describe_topic_create {

@Nested
@DisplayName("topicCreateRequestDto가 들어오면")
class Context_with_a_topicCreateRequestDto {

@Test
@DisplayName("토픽을 생성한다.")
public void it_returns_2XX_if_the_topic_was_created_successfully() {
TopicCreateRequest topicCreateRequest = getTopicCreateRequest();

Long savedTopicId = topicFacade.create(topicCreateRequest);

TopicDetailResponse topicById = topicFacade.findOne(savedTopicId);

Assertions.assertThat(topicById.title()).isEqualTo(topicCreateRequest.title());
}
}
}

@Nested
@DisplayName("토픽 수정 메서드는")
class Describe_topic_update {

@Nested
@DisplayName("TopicUpdateRequestDto가 들어오면")
class Context_with_a_TopicUpdateRequestDto {

@Test
@DisplayName("토픽 내용을 수정한다.")
public void it_returns_2XX_if_the_topic_is_modified() {
TopicCreateRequest topicCreateRequest = getTopicCreateRequest();
Long savedTopicId = topicFacade.create(topicCreateRequest);

TopicUpdateRequest topicUpdateRequest = TopicUpdateRequest.builder()
.title("1일 5커밋")
.description(topicA.getDescription())
.tags(topicA.getTags())
.pointPerPerson(topicA.getPointPerPerson())
.notice(topicA.getNotice()).build();

topicFacade.update(savedTopicId, topicUpdateRequest);

TopicDetailResponse findTopic = topicFacade.findOne(savedTopicId);
Assertions.assertThat(findTopic.title()).isEqualTo("1일 5커밋");
}
}
}

@Nested
@DisplayName("토픽 삭제 메서드는")
class Describe_topic_delete {

@Nested
@DisplayName("삭제할 토픽 Id가 주어질 때")
class Context_with_a_TopicUpdateRequestDto {

@Test
@DisplayName("해당 토픽을 삭제한다.")
public void it_returns_2XX_if_the_topic_is_successfully_deleted() throws Exception {
TopicCreateRequest topicCreateRequest = getTopicCreateRequest();
Long savedTopicId = topicFacade.create(topicCreateRequest);

topicFacade.delete(savedTopicId);

try {
topicFacade.findOne(savedTopicId);
} catch (BusinessException e) {
assertEquals("해당 토픽을 찾을 수 없습니다.", e.getMessage());
}
}
}
}
}

This file was deleted.

0 comments on commit a4c47ef

Please sign in to comment.