강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

soollung님의 프로필 이미지
soollung

작성한 질문수

스프링 시큐리티

Spring security test 수행시 @AuthenticationPrincipal 가 null 로 세팅됨

작성

·

534

0

안녕하세요

아래와 같이 Controller, 테스트 코드를 작성했습니다.

어플리케이션을 수행하고 로그인했을때는 정상적으로 사용자 정보가 출력이 되는데요

아래와 같이 테스트 코드를 수행했을때는 @AuthenticationPrincipal user 가 null 로 매핑이 됩니다.

해결 방법이 있을까요?

    @GetMapping(path="test")
    public String index(@AuthenticationPrincipal UserAccount user) {
        log.info(user.getUser());
        return "/index";
    }
    @Test
    public void index() throws Exception {
        // Given, When, Then
        mockMvc.perform(get("/test")
                .with(user("user").password("pwd").roles("USER")))
                .andExpect(status().isOk())
                .andExpect(authenticated())
        ;
    }

답변 1

1

백기선님의 프로필 이미지
백기선
지식공유자

테스트 코드 설정이 뭔가 잘못된것 같네요. 스프링 시큐리티가 적용된 테스트 인지 확인해 보실 필요가 있겠습니다.

soollung님의 프로필 이미지
soollung

작성한 질문수

질문하기