Skip to content

Commit

Permalink
[TDD스터디] MemberRepository, Service, Controller 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sunseo18 committed Dec 12, 2023
1 parent d4e81ab commit a0197a2
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.SecondSeminar.member.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatList;
import static org.mockito.BDDMockito.given;

import com.example.SecondSeminar.member.domain.Member;
import com.example.SecondSeminar.member.domain.MemberJpaRepository;
import com.example.SecondSeminar.member.domain.Part;
import com.example.SecondSeminar.member.domain.SOPT;
import com.example.SecondSeminar.member.dto.request.MemberProfileUpdateRequest;
import com.example.SecondSeminar.member.dto.response.MemberGetResponse;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MemberServiceTest {

@Mock
MemberJpaRepository memberRepository;

@InjectMocks
private MemberService memberService;

@Test
void getMemberByIdV2() {
Long memberId = 1L;
SOPT soptInfo = new SOPT(31, Part.SERVER);
Member member = new Member("정준서", "DDD", 24, soptInfo);
given(memberRepository.findByIdOrThrowException(memberId)).willReturn(member);

MemberGetResponse response = memberService.getMemberByIdV2(memberId);
assertThat(response.name()).isEqualTo("정준서");
assertThat(response.nickname()).isEqualTo("DDD");
assertThat(response.age()).isEqualTo(24);
assertThat(response.soptInfo()).isEqualTo(soptInfo);
}


@Test
void getMembers() {
Member member1 = new Member("정준서", "DDD", 24, new SOPT(31, Part.SERVER));
Member member2 = new Member("김태욱", "발라더", 25, new SOPT(33, Part.SERVER));
Member member3 = new Member("정홍준", "아빠", 27, new SOPT(32, Part.SERVER));

given(memberRepository.findAll()).willReturn(List.of(member1, member2, member3));

List<MemberGetResponse> responseList = memberService.getMembers();
assertThat(responseList).isInstanceOf(List.class);
assertThatList(responseList).extracting("name").contains("정준서", "김태욱", "정홍준");
assertThatList(responseList).extracting("nickname").contains("DDD", "발라더", "아빠");
assertThatList(responseList).extracting("age").contains(24, 25, 27);
}

@Test
void updateSOPT() {
Long memberId = 1L;
SOPT soptInfo = new SOPT(31, Part.SERVER);
Member member = new Member("정준서", "DDD", 24, soptInfo);
MemberProfileUpdateRequest request = new MemberProfileUpdateRequest(33, Part.SERVER);

given(memberRepository.findByIdOrThrowException(memberId)).willReturn(member);

memberService.updateSOPT(1L, request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.SecondSeminar.member.domain;

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

import com.example.SecondSeminar.member.exception.MemberException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles("test")
@DataJpaTest
public class MemberJpaRepositoryTest {
@Autowired
private MemberJpaRepository memberJpaRepository;

@Test
void findExistingMemberById() {
Long id = 1L;
Member member = memberJpaRepository.findByIdOrThrowException(id);
assertThat(member).isNotNull();
assertThat(member.getId()).isEqualTo(1L);
}

@Test
void findNotExistingMemberById() {
Long id = 0L;
assertThatExceptionOfType(MemberException.class).isThrownBy(
() -> memberJpaRepository.findByIdOrThrowException(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.SecondSeminar.member.presentation;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.example.SecondSeminar.member.application.MemberService;
import com.example.SecondSeminar.member.domain.Part;
import com.example.SecondSeminar.member.domain.SOPT;
import com.example.SecondSeminar.member.dto.response.MemberGetResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

@AutoConfigureMockMvc(addFilters = false)
@WebMvcTest(MemberController.class)
public class MemberControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private MemberService memberService;

private final static String accessToken = "Bearer (accessToken)";

@Test
void getMemberProfileV2() throws Exception {
Long memberId = 1L;
String url = String.format("/api/member/%d/v2", memberId);

MemberGetResponse response = new MemberGetResponse("정준서", "DDD", 24,
new SOPT(31, Part.SERVER));

given(memberService.getMemberByIdV2(any())).willReturn(response);

mvc.perform(get(url).header("Authorization", accessToken))
.andExpect(status().isOk())
.andExpect(content().json(
"{\"name\": \"정준서\", \"nickname\": \"DDD\", \"age\": 24, \"soptInfo\": {\"generation\": 31, \"part\": \"SERVER\"}}"))
.andDo(print());
}


}

0 comments on commit a0197a2

Please sign in to comment.