Skip to content

Commit

Permalink
Merge pull request #64 from wbluke/feature/63-service-test
Browse files Browse the repository at this point in the history
[#63] test: service 테스트 추가
  • Loading branch information
wbluke authored Aug 19, 2019
2 parents 13bf0ce + 5f29deb commit 26d86c7
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.mindrot:jbcrypt:0.3m'

developmentOnly 'org.springframework.boot:spring-boot-devtools'

runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation 'org.jmockit:jmockit:1.24'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.woowacourse.zzazanstagram.model.article.dto;

import java.time.LocalDateTime;
import java.util.Objects;

public class ArticleResponse {
private Long id;
Expand Down Expand Up @@ -112,4 +113,23 @@ public ArticleResponse build() {
return new ArticleResponse(id, image, contents, nickName, profileImage, createdDate, lastModifiedDate);
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArticleResponse response = (ArticleResponse) o;
return Objects.equals(id, response.id) &&
Objects.equals(image, response.image) &&
Objects.equals(contents, response.contents) &&
Objects.equals(nickName, response.nickName) &&
Objects.equals(profileImage, response.profileImage) &&
Objects.equals(createdDate, response.createdDate) &&
Objects.equals(lastModifiedDate, response.lastModifiedDate);
}

@Override
public int hashCode() {
return Objects.hash(id, image, contents, nickName, profileImage, createdDate, lastModifiedDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.woowacourse.zzazanstagram.model.member.domain.vo.*;

import javax.persistence.Entity;
import java.util.Objects;

@Entity
public class Member extends BaseEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;

@Embeddable
public class Email {
Expand Down Expand Up @@ -37,4 +38,17 @@ private boolean isMismatch(String email) {
public String getEmail() {
return email;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Email email1 = (Email) o;
return Objects.equals(email, email1.email);
}

@Override
public int hashCode() {
return Objects.hash(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;

@Embeddable
public class Name {
Expand Down Expand Up @@ -37,4 +38,17 @@ private boolean isMismatch(String name) {
public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Name name1 = (Name) o;
return Objects.equals(name, name1.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;

@Embeddable
public class NickName {
Expand Down Expand Up @@ -37,4 +38,17 @@ private boolean isMismatch(String name) {
public String getNickName() {
return nickName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NickName nickName1 = (NickName) o;
return Objects.equals(nickName, nickName1.nickName);
}

@Override
public int hashCode() {
return Objects.hash(nickName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;

@Embeddable
public class Password {
Expand Down Expand Up @@ -56,4 +57,17 @@ static boolean isMatch(String password, String hashed) {
return BCrypt.checkpw(password, hashed);
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Password password1 = (Password) o;
return Objects.equals(password, password1.password);
}

@Override
public int hashCode() {
return Objects.hash(password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;

@Embeddable
public class ProfileImage {
Expand All @@ -23,4 +24,17 @@ public static ProfileImage of(final String url) {
public String getUrl() {
return url;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProfileImage that = (ProfileImage) o;
return Objects.equals(url, that.url);
}

@Override
public int hashCode() {
return Objects.hash(url);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.woowacourse.zzazanstagram.model.member.dto;

import java.util.Objects;

public class MemberResponse {
private String nickName;
private String name;
Expand Down Expand Up @@ -44,4 +46,20 @@ public String getProfileImage() {
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MemberResponse that = (MemberResponse) o;
return Objects.equals(nickName, that.nickName) &&
Objects.equals(name, that.name) &&
Objects.equals(email, that.email) &&
Objects.equals(profileImage, that.profileImage);
}

@Override
public int hashCode() {
return Objects.hash(nickName, name, email, profileImage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
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);
}

}
Loading

0 comments on commit 26d86c7

Please sign in to comment.