Skip to content

Commit

Permalink
fix: test 내에서 Transactional로 롤백하던 것 DataInitializer로 대체
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Aug 10, 2023
1 parent a8fd1e2 commit 0c80f77
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ private RecommendedPost findPostOrThrow(final Long recommendedId) {
public void delete(final Long recommendedId) {
final RecommendedPost recommendedPost = findPostOrThrow(recommendedId);
recommendedPost.remove();
recommendedPostRepository.delete(recommendedPost);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.util.Objects;

@Entity
Expand Down Expand Up @@ -34,12 +40,7 @@ public void updateUrl(final String url) {
}

public void remove() {
if (this.keyword == null) {
return;
}

keyword.getRecommendedPosts().remove(this);
this.keyword = null;
}

public void addKeyword(final Keyword keyword) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package wooteco.prolog.roadmap.application;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import wooteco.prolog.common.DataInitializer;
import wooteco.prolog.roadmap.application.dto.RecommendedRequest;
import wooteco.prolog.roadmap.application.dto.RecommendedUpdateRequest;
import wooteco.prolog.roadmap.domain.Keyword;
Expand All @@ -21,7 +22,6 @@
import static org.assertj.core.api.SoftAssertions.assertSoftly;

@SpringBootTest
@Transactional
class RecommendedPostServiceTest {

@Autowired
Expand All @@ -33,6 +33,10 @@ class RecommendedPostServiceTest {
@Autowired
private SessionRepository sessionRepository;

@Autowired
private DataInitializer dataInitializer;


private Keyword keyword;

@BeforeEach
Expand All @@ -41,6 +45,11 @@ public void init() {
this.keyword = keywordRepository.save(Keyword.createKeyword("이름", "설명", 1, 1, session.getId(), null));
}

@AfterEach
public void removeAll() {
dataInitializer.execute();
}

@Test
@DisplayName("추천 포스트 생성 테스트")
void create() {
Expand Down Expand Up @@ -81,7 +90,7 @@ void update() {
@DisplayName("추천 포스트 삭제 테스트")
void delete() {
//given
final RecommendedRequest request = new RecommendedRequest("https//:example.com");
final RecommendedRequest request = new RecommendedRequest("https://example.com");
Long recommendedPostId = recommendedPostService.create(keyword.getId(), request);

//when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wooteco.prolog.roadmap.domain;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -13,21 +12,15 @@ class RecommendedPostTest {
@DisplayName("삭제 기능 테스트")
void remove() {
//given
final RecommendedPost recommendedPost = new RecommendedPost(1L, "https://example.com", null);
final Keyword keyword = Keyword.createKeyword("이름", "설명", 1, 1, 1L, null);
final RecommendedPost recommendedPost = new RecommendedPost(1L, "https://example.com", null);
recommendedPost.addKeyword(keyword);

//when
final Keyword before = recommendedPost.getKeyword();
recommendedPost.remove();
final Keyword after = recommendedPost.getKeyword();

//then
Assertions.assertAll(
() -> assertThat(before).isNotNull(),
() -> assertThat(after).isNull()
);

assertThat(keyword.getRecommendedPosts()).isEmpty();
}

@Test
Expand Down

0 comments on commit 0c80f77

Please sign in to comment.