forked from woowacourse/miniprojects-2019
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from wbluke/feature/63-service-test
[#63] test: service 테스트 추가
- Loading branch information
Showing
13 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,8 @@ public class ArticleConstant { | |
public static final String IMAGE_URL = "https://image.shutterstock.com/image-photo/white-transparent-leaf-on-mirror-600w-1029171697.jpg"; | ||
public static final String CONTENTS = "글의 내용이란다"; | ||
public static final String HASHTAG = "#아이크 #닉 #뚱이 #제이 #에헴"; | ||
public static final String EMAIL = "[email protected]"; | ||
public static final String NAME = "개똥이"; | ||
public static final String NICKNAME = "말똥이"; | ||
public static final String PASSWORD = "!#Password12"; | ||
} |
97 changes: 97 additions & 0 deletions
97
src/test/java/com/woowacourse/zzazanstagram/model/article/service/ArticleServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.woowacourse.zzazanstagram.model.article.service; | ||
|
||
import com.woowacourse.zzazanstagram.model.article.domain.Article; | ||
import com.woowacourse.zzazanstagram.model.article.domain.vo.Contents; | ||
import com.woowacourse.zzazanstagram.model.article.domain.vo.Image; | ||
import com.woowacourse.zzazanstagram.model.article.dto.ArticleRequest; | ||
import com.woowacourse.zzazanstagram.model.article.dto.ArticleResponse; | ||
import com.woowacourse.zzazanstagram.model.article.repository.ArticleRepository; | ||
import com.woowacourse.zzazanstagram.model.member.domain.Member; | ||
import com.woowacourse.zzazanstagram.model.member.service.MemberService; | ||
import mockit.Deencapsulation; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static com.woowacourse.zzazanstagram.model.article.ArticleConstant.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
class ArticleServiceTest { | ||
private Image image; | ||
private Contents contents; | ||
private Member member; | ||
|
||
@InjectMocks // @Mock, @Spy가 붙은 목 객체를 자신의 멤버 클래스와 일치하면 주입시킨다 | ||
private ArticleService articleService; | ||
|
||
@Mock | ||
private MemberService memberService; | ||
|
||
@Mock | ||
private ArticleRepository articleRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
image = Image.of(IMAGE_URL); | ||
contents = Contents.of(CONTENTS); | ||
member = Member.MemberBuilder.aMember() | ||
.email(EMAIL) | ||
.name(NAME) | ||
.nickName(NICKNAME) | ||
.password(PASSWORD) | ||
.profile(IMAGE_URL) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void 모든_게시글들이_select되는지_테스트() { // TODO 나중에 팔로우 중인 게시글만 뽑아내도록 바꿔야 함 | ||
// given | ||
Article article = new Article(image, contents, member); | ||
ArticleResponse response = Deencapsulation.invoke(ArticleAssembler.class, "toDto", article); | ||
List<Article> articles = Arrays.asList(article, article, article); | ||
|
||
given(articleRepository.findAllByOrderByIdDesc()).willReturn(articles); | ||
|
||
// when | ||
List<ArticleResponse> articleResponses = articleService.getArticleResponses(); | ||
|
||
// then | ||
assertThat(articleResponses).isEqualTo(Arrays.asList(response, response, response)); | ||
/*ArticleResponse resultElement = ArticleResponse.ArticleResponseBuilder.anArticleResponse() | ||
.id(article.getId()) | ||
.image(article.getImageValue()) | ||
.contents(article.getContentsValue()) | ||
.nickName(article.getAuthor().getNickNameValue()) | ||
.profileImage(article.getAuthor().getProfileImageValue()) | ||
.createdDate(article.getCreatedDate()) | ||
.lastModifiedDate(article.getLastModifiedDate()) | ||
.build();*/ | ||
} | ||
|
||
@Test | ||
public void save() { | ||
// given | ||
ArticleRequest articleRequest = new ArticleRequest(IMAGE_URL, CONTENTS, HASHTAG); | ||
Article article = Deencapsulation.invoke(ArticleAssembler.class, "toEntity", articleRequest, member); | ||
|
||
given(memberService.findMemberByEmail(EMAIL)).willReturn(member); | ||
given(articleRepository.save(article)).willReturn(article); | ||
|
||
// when | ||
articleService.save(articleRequest, EMAIL); | ||
|
||
// then | ||
verify(articleRepository, times(1)).save(article); | ||
} | ||
|
||
} |
Oops, something went wrong.