카테고리 없음
                
              Spring boot 2.1 의@WebMvcTest
                머룽
                 2023. 4. 23. 14:05
              
              
            
            오늘 이야기할 내용은 Spring boot 2.1의 
@WebMvcTest 어노테이션에 대해서 살펴볼텐데 기존의 @WebMvcTest과 조금 다른 부분이 있어 그것에 대해 알아보도록 하자.
만약 Spring boot 2.0 혹은 그 이전 버전에서 Spring boot 2.1로 버전을 올린다면 함께 봐야 할 수도 있다. 물론 그 상황이 라면? 
필자도 Spring boot 2.0 에서 2.1로 올렸을 때 발생한 이슈였다. 2.0에서는 문제 없이 잘 실행 되었지만 2.1로 버전을 올렸더니 갑자기 테스트 케이스들이 실패하였다.
그래서 그 이유가 무엇인지 찾아보기 시작했다.
일단 어떤 경우에 이런 현상이 나오는지 코드로 살펴보도록 하자. 이 코드는 단지 예제일 뿐이다.
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/hi").authenticated()
            .anyRequest()
            .permitAll()
            .and()
            .csrf()
            .disable();
    }
    @Bean
    public UserManager userManager(UserRepository userRepository) {
        return new UserManager(userRepository);
    }
}
UserManager 경우에는 Security 와 관련있는 어떤 클래스라고 생각하면 되겠다. 그래서 위와 같이 설정을 했다고 하자.
@RestController
public class HelloController {
    @GetMapping("hi")
    public String hi() {
        return "hi";
    }
}
@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
@WithMockUser
public class HelloControllerTests {
    @Autowired
    private MockMvc mockMvc;
    @Test
    public void hi() throws Exception {
        mockMvc.perform(get("/hi")).andExpect(status().isOk())
            .andExpect(content().string("hi"))
            .andDo(print());
    }
}
java.lang.IllegalStateException: Failed to load ApplicationContext
....
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com.example.demosecurity.UserRepository available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1644)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1203)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
UserManager의 디펜더시인 UserRepository타입의 빈을 찾을 수 없다는 내용이다. 그럼 왜 갑자기 이런 에러가 발생하는 걸까?
그 이유는 @WebMvcTest어노테이션으로 테스트할 때 기존에는 Spring boot의 기본 Config를 사용했다면 2.1부터는 WebSecurityConfigurer를 사용한 Config들도 같이 설정되어 실행된다. 그래서 위와 같이 UserRepository 라는 빈을 찾지 못한다고 에러가 발생된 것이다.
기존에는 설정해 놓은 프로젝트의 Security의 설정이 동작하는 것이 아니라 Spring boot 의 기본 설정이 동작해서 이 것이 정말 맞는 테스트인가? 의문도 생길수 있다.
아래의 코드를 살펴보자.
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/admin/**").access("hasRole(ADMIN)")
            .anyRequest()
            .permitAll()
            .and()
            .csrf()
            .disable();
    }
}
@RunWith(SpringRunner.class)
@WebMvcTest(AdminController.class)
public class AdminControllerTests {
    @Autowired
    private MockMvc mockMvc;
    @Test
    @WithMockUser(username = "example", roles = {"ANONYMOUS"})
    public void adminTestWithBadAuthentication() throws Exception {
        mockMvc.perform(get("/admin"))
            .andExpect(status().isForbidden());
    }
    @Test
    @WithMockUser(username = "user", roles = {"ADMIN"})
    public void adminTestWithAuthentication() throws Exception {
        mockMvc.perform(get("/admin"))
            .andExpect(status().isOk())
            .andExpect(content().string("admin"));
    }
}
ANONYMOUS 이니 403 에러가 떨어져야 정상이다. 왜냐하면 /admin/** 으로 들어올경우 role이 ADMIN 이어야 한다고 설정했으니 말이다.
.antMatchers("/admin/**").access("hasRole(ADMIN)")
adminTestWithAuthentication 메서드는 성공하나 adminTestWithBadAuthentication 메서드는 실패로 돌아간다.
java.lang.AssertionError: Status 
Expected :403
Actual   :200
@RunWith(SpringRunner.class)
@WebMvcTest(AdminController.class)
@Import(WebSecurityConfig.class)
public class AdminControllerTests {
  //...
}