-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
25 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -4,14 +4,20 @@ | |
import com.web.baebaeBE.domain.answer.dto.AnswerCreateRequest; | ||
import com.web.baebaeBE.domain.answer.dto.AnswerDetailResponse; | ||
import com.web.baebaeBE.domain.answer.dto.AnswerResponse; | ||
import com.web.baebaeBE.domain.answer.entity.Answer; | ||
import com.web.baebaeBE.domain.answer.repository.AnswerRepository; | ||
import com.web.baebaeBE.domain.answer.service.AnswerService; | ||
import com.web.baebaeBE.domain.member.entity.Member; | ||
import com.web.baebaeBE.domain.member.entity.MemberType; | ||
import com.web.baebaeBE.domain.member.repository.MemberRepository; | ||
import com.web.baebaeBE.domain.oauth2.controller.Oauth2Controller; | ||
import com.web.baebaeBE.domain.oauth2.service.KakaoClient; | ||
import com.web.baebaeBE.domain.oauth2.service.Oauth2Service; | ||
import com.web.baebaeBE.domain.question.entity.Question; | ||
import com.web.baebaeBE.domain.question.repository.QuestionJpaRepository; | ||
import com.web.baebaeBE.domain.question.repository.QuestionRepository; | ||
import com.web.baebaeBE.domain.reactioncount.entity.ReactionCount; | ||
import com.web.baebaeBE.global.authorization.aspect.AuthPolicyAspect; | ||
import com.web.baebaeBE.global.jwt.JwtTokenProvider; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -27,6 +33,10 @@ | |
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
@@ -35,12 +45,14 @@ | |
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
@@ -50,6 +62,7 @@ | |
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ActiveProfiles("test") | ||
@WithMockUser | ||
public class AnswerTest { | ||
@Autowired | ||
|
@@ -67,9 +80,16 @@ public class AnswerTest { | |
@MockBean | ||
private AnswerService answerService; | ||
|
||
@MockBean | ||
private AnswerRepository answerRepository; | ||
|
||
@Autowired | ||
private JwtTokenProvider tokenProvider; | ||
|
||
@MockBean | ||
private KakaoClient kakaoClient; | ||
@MockBean | ||
private Oauth2Service oauth2Service; | ||
@MockBean | ||
private Oauth2Controller oauth2Controller; | ||
|
||
|
@@ -79,45 +99,54 @@ public class AnswerTest { | |
private Member testReceiver; | ||
private String refreshToken; | ||
private Question testQuestion; | ||
private Answer testAnswer; | ||
|
||
@BeforeEach | ||
void setup() { | ||
// Mock 객체 초기화 | ||
testMember = Member.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.nickname("장지효") | ||
.memberType(MemberType.KAKAO) | ||
.refreshToken("null") | ||
.build(); | ||
|
||
testReceiver = Member.builder() | ||
.id(2L) | ||
.email("[email protected]") | ||
.nickname("장지효2") | ||
.memberType(MemberType.KAKAO) | ||
.refreshToken("null") | ||
.build(); | ||
|
||
// memberRepository.save() 메서드를 목킹하여 호출된 객체를 반환하도록 설정 | ||
when(memberRepository.save(any(Member.class))).thenAnswer(invocation -> invocation.getArgument(0)); | ||
|
||
// testMember와 testReceiver를 저장 | ||
testMember = memberRepository.save(testMember); | ||
testReceiver = memberRepository.save(testReceiver); | ||
when(memberRepository.save(any(Member.class))).thenReturn(testMember); | ||
when(memberRepository.findByEmail("[email protected]")).thenReturn(Optional.of(testMember)); | ||
when(memberRepository.findById(1L)).thenReturn(Optional.of(testMember)); | ||
|
||
// 토큰 생성 및 업데이트 | ||
refreshToken = tokenProvider.generateToken(testMember, Duration.ofDays(14)); | ||
testMember.updateRefreshToken(refreshToken); | ||
memberRepository.save(testMember); | ||
when(memberRepository.save(any(Member.class))).thenReturn(testReceiver); | ||
when(memberRepository.findByEmail("[email protected]")).thenReturn(Optional.of(testReceiver)); | ||
when(memberRepository.findById(1L)).thenReturn(Optional.of(testReceiver)); | ||
|
||
refreshToken = tokenProvider.generateToken(testMember, Duration.ofDays(14)); // 임시 refreshToken 생성 | ||
refreshToken = tokenProvider.generateToken(testReceiver, Duration.ofDays(14)); | ||
|
||
testMember.updateRefreshToken(refreshToken); | ||
when(memberRepository.save(testMember)).thenReturn(testMember); | ||
|
||
testReceiver.updateRefreshToken(refreshToken); | ||
memberRepository.save(testReceiver); | ||
when(memberRepository.save(testReceiver)).thenReturn(testReceiver); | ||
|
||
when(memberRepository.findByRefreshToken(refreshToken)).thenReturn(Optional.of(testMember)); | ||
when(memberRepository.findByRefreshToken(refreshToken)).thenReturn(Optional.of(testReceiver)); | ||
|
||
// memberRepository.save() 메서드를 목킹하여 호출된 객체를 반환하도록 설정 | ||
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0)); | ||
|
||
// testQuestion 저장 | ||
testQuestion = questionRepository.save(new Question(null, testMember, testReceiver, "이것은 질문입니다.", "장지효", true, LocalDateTime.now(), false)); | ||
|
||
} | ||
|
||
@AfterEach | ||
|
@@ -136,14 +165,14 @@ public void createAnswerTest() throws Exception { | |
MockMultipartFile imageFile = new MockMultipartFile("imageFile", "image.jpg", MediaType.IMAGE_JPEG_VALUE, "image content".getBytes()); | ||
MockMultipartFile requestFile = new MockMultipartFile("request", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsBytes(createRequest)); | ||
|
||
when(answerService.createAnswer(any(AnswerCreateRequest.class), eq(1L), any(MockMultipartFile.class))) | ||
when(answerService.createAnswer(any(AnswerCreateRequest.class), eq(testMember.getId()), any(MockMultipartFile.class))) | ||
.thenReturn(new AnswerDetailResponse( | ||
1L, testQuestion.getId(), testQuestion.getContent(),1L, "이것은 답변입니다.", | ||
testMember.getNickname(), "안녕",true, "https://link.com", "노래 제목", "가수 이름", "https://audio.url", | ||
1L, testQuestion.getId(), testQuestion.getContent(), 1L, "이것은 답변입니다.", | ||
testMember.getNickname(), "안녕", true, "https://link.com", "노래 제목", "가수 이름", "https://audio.url", | ||
"https://image.url", LocalDateTime.now() | ||
)); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/answers/{memberId}", 1L) | ||
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/answers/{memberId}", testMember.getId()) | ||
.file(imageFile) | ||
.file(requestFile) | ||
.header("Authorization", "Bearer " + refreshToken) | ||
|
@@ -223,6 +252,9 @@ public void updateAnswerTest() throws Exception { | |
@Test | ||
@DisplayName("답변 삭제 테스트(): 답변을 삭제한다.") | ||
public void deleteAnswerTest() throws Exception { | ||
|
||
doNothing().when(answerService).deleteAnswer(eq(1L)); | ||
|
||
mockMvc.perform(delete("/api/answers/{answerId}", 1L) | ||
.header("Authorization", "Bearer " + refreshToken) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
|
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
|
@@ -44,6 +45,7 @@ | |
@SpringBootTest() | ||
@AutoConfigureMockMvc | ||
@WithMockUser | ||
@ActiveProfiles("test") | ||
@Transactional | ||
public class QuestionTest { | ||
|
||
|
@@ -65,6 +67,7 @@ public class QuestionTest { | |
private Member testMember; | ||
private Member testReceiver; | ||
private String refreshToken; | ||
private String refreshTokenReceiver; | ||
private QuestionDetailResponse testQuestionDetailResponse; | ||
|
||
@BeforeEach | ||
|
@@ -85,17 +88,25 @@ void setup() { | |
.refreshToken("null") | ||
.build(); | ||
|
||
refreshToken = tokenProvider.generateToken(testMember, Duration.ofDays(14)); | ||
testMember.updateRefreshToken(refreshToken); | ||
memberRepository.save(testMember); | ||
when(memberRepository.save(any(Member.class))).thenReturn(testMember); | ||
when(memberRepository.findByEmail("[email protected]")).thenReturn(Optional.of(testMember)); | ||
when(memberRepository.findById(1L)).thenReturn(Optional.of(testMember)); | ||
|
||
when(memberRepository.save(any(Member.class))).thenReturn(testReceiver); | ||
when(memberRepository.findByEmail("[email protected]")).thenReturn(Optional.of(testReceiver)); | ||
when(memberRepository.findById(1L)).thenReturn(Optional.of(testReceiver)); | ||
|
||
refreshToken = tokenProvider.generateToken(testMember, Duration.ofDays(14)); // 임시 refreshToken 생성 | ||
refreshToken = tokenProvider.generateToken(testReceiver, Duration.ofDays(14)); | ||
testReceiver.updateRefreshToken(refreshToken); | ||
memberRepository.save(testReceiver); | ||
|
||
when(memberRepository.findByEmail("[email protected]")).thenReturn(Optional.of(testMember)); | ||
when(memberRepository.findByEmail("[email protected]")).thenReturn(Optional.of(testReceiver)); | ||
testMember.updateRefreshToken(refreshToken); | ||
when(memberRepository.save(testMember)).thenReturn(testMember); | ||
|
||
testReceiver.updateRefreshToken(refreshToken); | ||
when(memberRepository.save(testReceiver)).thenReturn(testReceiver); | ||
|
||
when(memberRepository.findByRefreshToken(refreshToken)).thenReturn(Optional.of(testMember)); | ||
when(memberRepository.findByRefreshToken(refreshToken)).thenReturn(Optional.of(testReceiver)); | ||
} | ||
|
||
@Test | ||
|
@@ -150,20 +161,21 @@ public void getQuestionsByMemberIdTest() throws Exception { | |
@DisplayName("질문 삭제 테스트(): 질문을 삭제한다.") | ||
public void deleteQuestionTest() throws Exception { | ||
|
||
String content = "이것은 삭제할 질문입니다."; | ||
|
||
// 질문 생성 및 저장 | ||
Question question = Question.builder() | ||
.id(1L) | ||
.sender(testMember) | ||
.receiver(testReceiver) | ||
.content(content) | ||
.content("이것은 삭제할 질문입니다.") | ||
.nickname("닉네임") | ||
.profileOnOff(true) | ||
.createdDate(LocalDateTime.now()) | ||
.build(); | ||
question.setId(1L); | ||
|
||
when(questionRepository.save(any(Question.class))).thenReturn(question); | ||
question = questionRepository.save(question); | ||
|
||
when(questionRepository.findById(1L)).thenReturn(Optional.of(question)); | ||
doNothing().when(questionService).deleteQuestion(eq(1L)); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.delete("/api/questions/{questionId}", question.getId()) | ||
|
@@ -172,3 +184,4 @@ public void deleteQuestionTest() throws Exception { | |
.andExpect(status().isNoContent()); | ||
} | ||
} | ||
|