[트러블슈팅/SpringBoot] WebMvcTest로 Controller 테스트 시 JwtUtil 인식 안 됨

2026. 3. 11. 12:09·트러블슈팅

문제

이미지와 함께 게시글 업로드 기능 관련 테스트 코드를 작성하는데, 계속 JwtUtil을 발견할 수 없다는 에러가 뜸

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
Error creating bean with name 'jwtAuthenticationFilter'
...
Caused by: NoSuchBeanDefinitionException:
No qualifying bean of type 'com.ahy.diarybackend.security.JwtUtil' available

 

게시글 관련 controller에서 사용자가 로그인 상태일 때만 게시글이 업로드 되도록 설정해두었는데,
Controller 테스트에선 JWT 검증이 필요 없도록 @AutoConfigureMockMvc(addFilters = false)로 Security 필터를 껐음에도 계속 같은 오류가 떴다

 

원인

결론은 SecurityConfig를 직접 작성했기 때문

 

테스트 코드를 작성할 때 @WebMvcTest(DiaryController.class)로 작성을 했는데,
이는 컨트롤러 관련 Bean만 로딩을 하기 때문에 Security 관련 Bean은 일부만 로딩되면서 의존성이 깨질 수 있다 한다


필터를 껐음에도 Spring Security가 실행되는 이유는 addFilters = false가 MockMvc 요청 시 Filter 실행만 끄는 것이기 때문에 Spring Security 자체는 여전히 ApplicationContext에 로딩됨

SpringSecurity를 끄기 위해 @WebMvcTest에 excludeAutoConfiguration = SecurityAutoConfiguration.class 값을 줘도 이 설정은 Spring Boot가 자동으로 등록하는 Security 설정만 제외함

 

JwtAuthenticationFilter가 @Component로 등록되어 있는 경우

ComponentScan --> JwtAuthenticationFilter 발견 -> Bean 생성 -> JwtUtil 필요 -> 에러

 

해결

간단하게 필요한 것들을 @MockBean으로 등록하면 됨

@MockBean
private JwtUtil jwtUtil;

@MockBean
private UserDetailsService userDetailsService;

 

결과

작성한 2개의 테스트가 모두 잘 실행됨

저작자표시 (새창열림)

'트러블슈팅' 카테고리의 다른 글

[트러블슈팅/SpringBoot] Nexacro 연동 시 발생한 의존성 문제  (0) 2026.06.12
[트러블슈팅/SpringBoot] 415 오류: Content-Type 'application/octet-stream' is not supported  (0) 2026.05.19
[트러블슈팅/SpringBoot] Builder 필드 초기값 누락  (0) 2026.01.22
[트러블슈팅/GitHub] 원격저장소 - 로컬저장소 기록(history) 병합  (0) 2026.01.17
[트러블슈팅/SpringBoot] Error creating bean with name 'entityManagerFactory' defined in class path resource  (0) 2024.12.05
'트러블슈팅' 카테고리의 다른 글
  • [트러블슈팅/SpringBoot] Nexacro 연동 시 발생한 의존성 문제
  • [트러블슈팅/SpringBoot] 415 오류: Content-Type 'application/octet-stream' is not supported
  • [트러블슈팅/SpringBoot] Builder 필드 초기값 누락
  • [트러블슈팅/GitHub] 원격저장소 - 로컬저장소 기록(history) 병합
naahy
naahy
  • naahy
    종합장
    naahy
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Java
      • SpringBoot
      • Git
      • 트러블슈팅
      • Vue.js
      • Kotlin
      • Node.js
      • 코딩테스트
        • 백준
        • 프로그래머스
      • 스터디
        • CS
        • 알고리즘
      • -
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 인기 글

  • 태그

    API
    백트래킹
    이분탐색
    React
    Kotlin
    코틀린
    mongodb
    node.js
    Java
    SpringBoot
    github
    프로그래머스
    백준
    트러블슈팅
    자바
    bfs
    Nexacro
    cs
    브루트포스
    투포인터
  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
naahy
[트러블슈팅/SpringBoot] WebMvcTest로 Controller 테스트 시 JwtUtil 인식 안 됨
상단으로

티스토리툴바