@Test
public void 계좌목록보기_유저별_test() throws Exception {
// given
Long userId = 1L;
// stub 1
User ssar = newMockUser(userId, "ssar", "쌀");
when(userRepository.findById(userId)).thenReturn(Optional.of(ssar));
// stub 2
Account ssarAccount1 = newMockAccount(1L, 1111L, 1000L, ssar);
Account ssarAccount2 = newMockAccount(2L, 2222L, 1000L, ssar);
List<Account> accountList = Arrays.asList(ssarAccount1, ssarAccount2);
when(accountRepository.findByUser_id(any())).thenReturn(accountList);
// when
AccountListResponseDto accountListResponseDto = accountService.계좌목록보기_유저별(userId);
// then
assertThat(accountListResponseDto.getFullname()).isEqualTo("쌀");
assertThat(accountListResponseDto.getAccounts().size()).isEqualTo(2);
}
어차피 Repo 테스트가 아니니까 stub을 2개를 걸어준다.
@WithUserDetails(value = "ssar", setupBefore = TestExecutionEvent.TEST_EXECUTION)
@Test
public void findUserAccount_test() throws Exception {
// given
// when
ResultActions resultActions = mvc.perform(get("/api/s/account/login-user"));
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);
// then
resultActions.andExpect(status().isOk());
}
본 코드를 보면 파라미터값이 @AuthenticationPrincipal LoginUser loginUser 이기때문에 아무것도 존재하지 않는다.