Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] MemberController 관련 테스트 코드 작성 #333

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package swm_nm.morandi.domain.member.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -8,7 +7,6 @@
import swm_nm.morandi.global.exception.MorandiException;
import swm_nm.morandi.global.exception.errorcode.MemberErrorCode;
import swm_nm.morandi.global.utils.SecurityUtils;

import javax.transaction.Transactional;

@Service
Expand All @@ -18,10 +16,10 @@ public class MemberEditService {

private final MemberRepository memberRepository;
@Transactional
public void editProfile(String nickname, String bojId) {
public void editProfile(String introduceInfo, String bojId) {
Long memberId = SecurityUtils.getCurrentMemberId();
Member member = memberRepository.findById(memberId).orElseThrow(
() -> new MorandiException(MemberErrorCode.MEMBER_NOT_FOUND));
member.editProfile(nickname, bojId);
member.editProfile(introduceInfo, bojId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
import swm_nm.morandi.global.exception.errorcode.AuthErrorCode;
import swm_nm.morandi.global.utils.SecurityUtils;

import javax.transaction.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
public class MemberInitService {

private final MemberRepository memberRepository;

@Transactional
public RegisterInfoDto memberInitialize(RegisterInfoDto registerInfoDto) {
Long userId = SecurityUtils.getCurrentMemberId();
Member member = memberRepository.findById(userId).orElseThrow(()-> new MorandiException(AuthErrorCode.MEMBER_NOT_FOUND));
Long memberId = SecurityUtils.getCurrentMemberId();
Member member = memberRepository.findById(memberId).orElseThrow(()-> new MorandiException(AuthErrorCode.MEMBER_NOT_FOUND));
member.setBojId(registerInfoDto.getBojId());
memberRepository.save(member);
return registerInfoDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package swm_nm.morandi.domain.member.service;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.transaction.annotation.Transactional;
import swm_nm.morandi.domain.member.dto.MemberInfoDto;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.member.repository.MemberRepository;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@WithMockUser(username = "1", roles = "USER")
class MemberEditServiceTest {

@Autowired
private MemberEditService memberEditService;

@Autowired
private MemberRepository memberRepository;
@Test
@Transactional
void editProfile() {
// given
MemberInfoDto memberInfoDto = new MemberInfoDto();
memberInfoDto.setIntroduceInfo("hello world");
memberInfoDto.setBojId("aj4941");

// when
memberEditService.editProfile(memberInfoDto.getIntroduceInfo(), memberInfoDto.getBojId());

// then
Optional<Member> result = memberRepository.findById(1L);
Member findMember = result.get();

assertThat(findMember.getIntroduceInfo()).isEqualTo("hello world");
assertThat(findMember.getBojId()).isEqualTo("aj4941");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package swm_nm.morandi.domain.member.service;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.transaction.annotation.Transactional;
import swm_nm.morandi.domain.member.dto.MemberInfoDto;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.member.repository.MemberRepository;

import java.util.Optional;

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

@SpringBootTest
@WithMockUser(username = "1", roles = "USER")
class MemberInfoServiceTest {

@Autowired
private MemberInfoService memberInfoService;

@Autowired
private MemberRepository memberRepository;
@Test
@Transactional
void getMemberInfo() {
// given
Optional<Member> result = memberRepository.findById(1L);
Member member = result.get();

// when
MemberInfoDto memberInfo = memberInfoService.getMemberInfo();

// then
assertThat(memberInfo.getBojId()).isEqualTo(member.getBojId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package swm_nm.morandi.domain.member.service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.transaction.annotation.Transactional;
import swm_nm.morandi.domain.member.dto.RegisterInfoDto;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.member.repository.MemberRepository;

import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@WithMockUser(username = "1", roles = "USER")
class MemberInitServiceTest {

@Autowired
private MemberRepository memberRepository;

@Autowired
private MemberInitService memberInitService;
@Test
void memberInitialize() {
// given
RegisterInfoDto registerInfoDto = RegisterInfoDto.builder()
.bojId("aj4941")
.build();

// when
memberInitService.memberInitialize(registerInfoDto);

// then
Optional<Member> result = memberRepository.findById(1L);
Member findMember = result.get();
assertThat(findMember.getBojId()).isEqualTo("aj4941");
}
}
Loading