문제
이미지와 함께 게시글 업로드 기능 관련 테스트 코드를 작성하는데, 계속 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 |