Skip to content

Commit

Permalink
[TEST] 테스트 에러 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Dec 29, 2023
1 parent 0fd7acf commit 6fe2631
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile("!test")
@EnableRabbit
@Configuration
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.twtw.backend.domain.friend.repository;

import static org.assertj.core.api.Assertions.assertThat;

import com.twtw.backend.domain.friend.entity.Friend;
import com.twtw.backend.domain.friend.entity.FriendStatus;
import com.twtw.backend.domain.member.entity.AuthType;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.entity.OAuth2Info;
import com.twtw.backend.domain.member.repository.MemberRepository;
import com.twtw.backend.support.repository.RepositoryTest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("FriendRepository에")
class FriendRepositoryTest extends RepositoryTest {

Expand All @@ -29,10 +28,10 @@ void save() {
// given
final Member from =
memberRepository.save(
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE)));
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE), "test"));
final Member to =
memberRepository.save(
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO)));
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO), "deviceToken"));
final Friend friend = new Friend(from, to);

// when
Expand All @@ -48,10 +47,10 @@ void findByTwoMemberId() {
// given
final Member from =
memberRepository.save(
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE)));
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE), "deviceToken1"));
final Member to =
memberRepository.save(
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO)));
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO), "deviceToken2"));
final Friend friend = new Friend(from, to);
final Friend expected = friendRepository.save(friend);

Expand All @@ -69,10 +68,10 @@ void findByMember() {
// given
final Member from =
memberRepository.save(
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE)));
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE), "deviceToken1"));
final Member to =
memberRepository.save(
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO)));
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO), "deviceToken2"));
final Friend friend = new Friend(from, to);
final Friend expected = friendRepository.save(friend);
expected.updateStatus(FriendStatus.ACCEPTED);
Expand All @@ -90,10 +89,10 @@ void findByMemberAndFriendStatus() {
// given
final Member from =
memberRepository.save(
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE)));
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE), "deviceToken1"));
final Member to =
memberRepository.save(
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO)));
new Member("2", "1234", new OAuth2Info("123", AuthType.KAKAO), "deviceToken2"));
final Friend friend = new Friend(from, to);
friendRepository.save(friend);

Expand All @@ -111,11 +110,11 @@ void findByMemberAndMemberNickname() {
// given
final Member from =
memberRepository.save(
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE)));
new Member("1", "123", new OAuth2Info("321", AuthType.APPLE), "deviceToken1"));
final String friendNickname = "2";
final Member to =
memberRepository.save(
new Member(friendNickname, "1234", new OAuth2Info("123", AuthType.KAKAO)));
new Member(friendNickname, "1234", new OAuth2Info("123", AuthType.KAKAO), "deviceToken2"));
final Friend friend = new Friend(from, to);
final Friend expected = friendRepository.save(friend);
expected.updateStatus(FriendStatus.ACCEPTED);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.twtw.backend.domain.friend.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.twtw.backend.domain.friend.dto.request.FriendRequest;
import com.twtw.backend.domain.friend.dto.request.FriendUpdateRequest;
import com.twtw.backend.domain.friend.dto.response.FriendResponse;
Expand All @@ -12,14 +10,15 @@
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.entity.OAuth2Info;
import com.twtw.backend.support.service.LoginTest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("FriendService의")
class FriendServiceTest extends LoginTest {

Expand All @@ -32,7 +31,7 @@ void addRequest() {
// given
final UUID id =
memberRepository
.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE)))
.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE), "deviceToken"))
.getId();

// when
Expand All @@ -49,7 +48,7 @@ void addRequest() {
void updateStatus() {
// given
final Member toMember =
memberRepository.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE)));
memberRepository.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE), "deviceToken"));
final Friend friend = friendRepository.save(new Friend(loginUser, toMember));

// when
Expand All @@ -66,7 +65,7 @@ void updateStatus() {
void getFriends() {
// given
final Member toMember =
memberRepository.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE)));
memberRepository.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE), "deviceToken"));
friendRepository.save(new Friend(loginUser, toMember));
friendService.updateStatus(
new FriendUpdateRequest(toMember.getId(), FriendStatus.ACCEPTED));
Expand All @@ -83,7 +82,7 @@ void getFriends() {
void getFriendsByStatus() {
// given
final Member toMember =
memberRepository.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE)));
memberRepository.save(new Member("1", "12", new OAuth2Info("123", AuthType.APPLE), "deviceToken"));
friendRepository.save(new Friend(loginUser, toMember));

// when
Expand All @@ -101,7 +100,7 @@ void getFriendByNickname() {
final String nickname = "1";
final Member toMember =
memberRepository.save(
new Member(nickname, "12", new OAuth2Info("123", AuthType.APPLE)));
new Member(nickname, "12", new OAuth2Info("123", AuthType.APPLE), "deviceToken"));
final Friend expected = friendRepository.save(new Friend(loginUser, toMember));
expected.updateStatus(FriendStatus.ACCEPTED);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package com.twtw.backend.domain.member.controller;

import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest;
import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.twtw.backend.domain.member.dto.request.DeviceTokenRequest;
import com.twtw.backend.domain.member.dto.request.MemberSaveRequest;
import com.twtw.backend.domain.member.dto.request.OAuthRequest;
Expand All @@ -22,14 +10,24 @@
import com.twtw.backend.domain.member.entity.AuthType;
import com.twtw.backend.domain.member.service.AuthService;
import com.twtw.backend.support.docs.RestDocsTest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.ResultActions;

import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest;
import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@DisplayName("AuthController의")
@WebMvcTest(AuthController.class)
class AuthControllerTest extends RestDocsTest {
Expand Down Expand Up @@ -102,6 +100,7 @@ void saveMember() throws Exception {
new MemberSaveRequest(
"정해진",
"http://some-url-to-profile-image",
"DEVICE_TOKEN",
new OAuthRequest(
"client-id", AuthType.APPLE))))
.contentType(MediaType.APPLICATION_JSON));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package com.twtw.backend.domain.member.repository;

import static org.assertj.core.api.Assertions.assertThat;

import com.twtw.backend.domain.member.dto.request.DeviceTokenRequest;
import com.twtw.backend.domain.member.entity.DeviceToken;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.fixture.member.MemberEntityFixture;
import com.twtw.backend.support.repository.RepositoryTest;

import jakarta.persistence.EntityManager;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("MemberRepository의")
public class MemberRepositoryTest extends RepositoryTest {
class MemberRepositoryTest extends RepositoryTest {

@Autowired private MemberRepository memberRepository;

@Autowired private DeviceTokenRepository deviceTokenRepository;

@Autowired private EntityManager em;

@Test
Expand Down Expand Up @@ -65,7 +61,6 @@ void saveDeivceToken() {
// when
DeviceToken deviceToken = new DeviceToken(deviceTokenRequest.getDeviceToken());
member.updateDeviceToken(deviceToken);
deviceTokenRepository.save(deviceToken);

// then
assertThat(member.getDeviceToken().getDeviceToken().equals("THIS_IS_TEST_TOKEN"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.twtw.backend.domain.member.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.twtw.backend.domain.member.dto.request.MemberSaveRequest;
import com.twtw.backend.domain.member.dto.request.OAuthRequest;
import com.twtw.backend.domain.member.dto.response.AfterLoginResponse;
Expand All @@ -13,11 +11,12 @@
import com.twtw.backend.fixture.member.MemberEntityFixture;
import com.twtw.backend.support.database.DatabaseTest;
import com.twtw.backend.support.exclude.ExcludeTest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.assertj.core.api.Assertions.assertThat;

@DatabaseTest
@DisplayName("AuthService의 ")
public class AuthServiceTest extends ExcludeTest {
Expand All @@ -36,6 +35,7 @@ void saveMemberKakao() {
new MemberSaveRequest(
"JinJooWon_Kakao",
"TEST_PROFILE_IMAGE",
"deviceToken",
new OAuthRequest("TEST_KAKAO_TOKEN", AuthType.KAKAO));

// when
Expand All @@ -53,13 +53,14 @@ void saveMemberApple() {
new MemberSaveRequest(
"JinJooWon_Apple",
"TEST_PROFILE_IMAGE",
"deviceToken",
new OAuthRequest("TEST_APPLE_TOKEN", AuthType.APPLE));

// when
AfterLoginResponse response = authService.saveMember(appleRequest);

// then
assertThat(response.getStatus().equals(AuthStatus.SIGNIN)).isTrue();
assertThat(response.getStatus()).isEqualTo(AuthStatus.SIGNIN);
}

@Test
Expand All @@ -76,6 +77,6 @@ void getTokenByOAuthSuccess() {
AfterLoginResponse response = authService.getTokenByOAuth(request);

// then
assertThat(response.getStatus().equals(AuthStatus.SIGNIN)).isTrue();
assertThat(response.getStatus()).isEqualTo(AuthStatus.SIGNIN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import com.twtw.backend.domain.member.entity.AuthType;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.entity.OAuth2Info;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum MemberEntityFixture {
LOGIN_MEMBER("호즨이", "http://someUrlToS3", "12345", AuthType.APPLE),
FIRST_MEMBER("담이와써요", "http://myPictureIsExpensive", "777", AuthType.APPLE),
SECOND_MEMBER("홍담진", "http://fighting", "1234567", AuthType.KAKAO);
LOGIN_MEMBER("호즨이", "http://someUrlToS3", "12345", "device", AuthType.APPLE),
FIRST_MEMBER("담이와써요", "http://myPictureIsExpensive", "777", "deviceToken", AuthType.APPLE),
SECOND_MEMBER("홍담진", "http://fighting", "1234567", "DeviceToken!", AuthType.KAKAO);

private final String name;
private final String profileImage;
private final String clientId;
private final String deviceToken;
private final AuthType authType;

public Member toEntity() {
return new Member(name, profileImage, new OAuth2Info(clientId, authType));
return new Member(name, profileImage, new OAuth2Info(clientId, authType), deviceToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import com.twtw.backend.config.rabbitmq.RabbitMQConfig;
import com.twtw.backend.domain.location.controller.LocationController;

import com.twtw.backend.domain.notification.messagequeue.FcmProducer;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.actuate.autoconfigure.amqp.RabbitHealthContributorAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;

@ActiveProfiles("test")
public abstract class ExcludeTest {

Expand All @@ -22,4 +26,11 @@ public abstract class ExcludeTest {
private RabbitHealthContributorAutoConfiguration rabbitHealthContributorAutoConfiguration;

@MockBean private LocationController locationController;

@MockBean private FcmProducer fcmProducer;

@BeforeEach
void setUp() {
doNothing().when(fcmProducer).sendNotification(any());
}
}

0 comments on commit 6fe2631

Please sign in to comment.