1. 요구사항
- @AuthenticationPrincipal로 인증된 페이지의 계정정보를 불러와야 한다.
- 해당 어노테이션으로 지정된 객체가 MemberDetails 이므로 이것을 불러올 때 null값이 되어선 안된다.
2. 해결사항
@WithUserDetails로 @AuthenticationPrincipal을 거친 계정정보를 불러올 수 있다.
@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class SellerControllerTests {
@Autowired
private MockMvc mockMvc;
@Test
// @AuthenticationPrincipal으로 인증된 페이지의 계정정보를 불러온다.(MemberDetails 객체)
@WithUserDetails("prism")
public void getMappingTest() throws Exception {
// 판매자 마이페이지
mockMvc.perform(MockMvcRequestBuilders.get("/seller/mypage/prdct")).andExpect(MockMvcResultMatchers.status().isOk());
}
}
3. 출처
Eliecer Hernández님 블로그(eliux.github.io/java/spring/testing/how-to-mock-authentication-in-spring/)
'Self-Study > 계란으로 바위치기' 카테고리의 다른 글
[Electron] 클라이언트 닫기가 안될 때 (0) | 2022.07.04 |
---|---|
[Electron] Unable to install dev tools 문제 (0) | 2022.07.04 |
인증이 필요한 페이지를 상대로 통합 테스트 진행하기 (0) | 2021.04.19 |
[Spring] 상품 찜하기 기능 (0) | 2021.04.16 |
[Spring] 해당 댓글을 작성한 회원만 삭제하는 기능 (0) | 2021.04.16 |
최근댓글