forked from Kernel360/f1-Yigil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: write test code to test an api for access my page data
- Loading branch information
1 parent
f21c9e4
commit 9f965c3
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/kr/co/yigil/global/config/LoginResolverConfig.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,21 @@ | ||
package kr.co.yigil.global.config; | ||
|
||
import java.util.List; | ||
import java.util.logging.Handler; | ||
import kr.co.yigil.login.application.LoginArgumentResolver; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class LoginResolverConfig implements WebMvcConfigurer { | ||
|
||
private final LoginArgumentResolver loginArgumentResolver; | ||
|
||
@Override | ||
public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(loginArgumentResolver); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/kr/co/yigil/login/application/LoginArgumentResolver.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,35 @@ | ||
package kr.co.yigil.login.application; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpSession; | ||
import kr.co.yigil.auth.domain.Accessor; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
@Component | ||
public class LoginArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterType().equals(Accessor.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
HttpSession session = request.getSession(false); | ||
|
||
if (session == null) return Accessor.guest(); | ||
|
||
Long memberId = (Long) session.getAttribute("memberId"); | ||
if (memberId == null) return Accessor.guest(); | ||
|
||
return Accessor.member(memberId); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
backend/src/test/java/kr/co/yigil/member/application/MemberServiceTest.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,53 @@ | ||
package kr.co.yigil.member.application; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import kr.co.yigil.member.domain.Member; | ||
import kr.co.yigil.member.domain.repository.MemberRepository; | ||
import kr.co.yigil.member.dto.response.MemberInfoResponse; | ||
import kr.co.yigil.post.domain.Post; | ||
import kr.co.yigil.post.domain.repository.PostRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class MemberServiceTest { | ||
|
||
@Mock | ||
private MemberRepository memberRepository; | ||
|
||
@Mock | ||
private PostRepository postRepository; | ||
|
||
@InjectMocks | ||
private MemberService memberService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@DisplayName("유효한 사용자ID가 주어졌을 때 사용자 정보가 잘 반환되는 지") | ||
@Test | ||
void whenGetMemberInfo_thenReturnsMemberInfoResponse_withValidMemberInfo() { | ||
Long memberId = 1L; | ||
Member mockMember = new Member("[email protected]", "123456", "stone", "profile.jpg", "kakao"); | ||
List<Post> mockPostList = new ArrayList<>(); | ||
|
||
when(memberRepository.findById(memberId)).thenReturn(Optional.of(mockMember)); | ||
when(postRepository.findAllByMember(mockMember)).thenReturn(mockPostList); | ||
|
||
MemberInfoResponse response = memberService.getMemberInfo(memberId); | ||
|
||
assertThat(response).isNotNull(); | ||
assertThat(response.getNickname()).isEqualTo("stone"); | ||
assertThat(response.getProfileImageUrl()).isEqualTo("profile.jpg"); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
backend/src/test/java/kr/co/yigil/member/presentation/MemberControllerTest.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,54 @@ | ||
package kr.co.yigil.member.presentation; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
||
import kr.co.yigil.auth.domain.Accessor; | ||
import kr.co.yigil.member.application.MemberService; | ||
import kr.co.yigil.member.dto.response.MemberInfoResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
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.http.MediaType; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@WebMvcTest(MemberController.class) | ||
public class MemberControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private MemberService memberService; | ||
|
||
@InjectMocks | ||
private MemberController memberController; | ||
|
||
@BeforeEach | ||
void setUp(WebApplicationContext webApplicationContext) { | ||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); | ||
} | ||
|
||
@DisplayName("내 정보가 조회될 때 200 응답과 response가 잘 반환되는지") | ||
@Test | ||
void whenGetMyInfo_thenReturns200AndMemberInfoResponse() throws Exception { | ||
MemberInfoResponse mockResponse = new MemberInfoResponse(); | ||
Accessor accessor = Accessor.member(1L); | ||
|
||
given(memberService.getMemberInfo(accessor.getMemberId())).willReturn(mockResponse); | ||
|
||
mockMvc.perform(get("/api/v1/member") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()); | ||
} | ||
} |